| 1 | #!/usr/local/bin/perl
 | 
|---|
| 2 | # 
 | 
|---|
| 3 | #@(#) smb-wall.pl Description:
 | 
|---|
| 4 | #@(#) A perl script which allows you to announce whatever you choose to
 | 
|---|
| 5 | #@(#) every PC client currently connected to a Samba Server...
 | 
|---|
| 6 | #@(#) ...using "smbclient -M" message to winpopup service.
 | 
|---|
| 7 | #@(#) Default usage is to message every connected PC.
 | 
|---|
| 8 | #@(#) Alternate usage is to message every pc on the argument list.
 | 
|---|
| 9 | #@(#)  Hacked up by Keith Farrar <farrar@parc.xerox.com>
 | 
|---|
| 10 | #
 | 
|---|
| 11 | # Cleanup and corrections by
 | 
|---|
| 12 | # Michal Jaegermann <michal@ellpspace.math.ualberta.ca>
 | 
|---|
| 13 | # Message to send can be now also fed (quietly) from stdin; a pipe will do.
 | 
|---|
| 14 | #=============================================================================
 | 
|---|
| 15 | 
 | 
|---|
| 16 | $smbstatus = "/usr/local/bin/smbstatus";
 | 
|---|
| 17 | $smbshout = "/usr/local/bin/smbclient -M";
 | 
|---|
| 18 | 
 | 
|---|
| 19 | if (@ARGV) {
 | 
|---|
| 20 |     @clients = @ARGV;
 | 
|---|
| 21 |     undef @ARGV;
 | 
|---|
| 22 | }
 | 
|---|
| 23 | else {  # no clients specified explicitly
 | 
|---|
| 24 |     open(PCLIST, "$smbstatus |") || die "$smbstatus failed!.\n$!\n";
 | 
|---|
| 25 |     while(<PCLIST>) {
 | 
|---|
| 26 |         last if /^Locked files:/;
 | 
|---|
| 27 |         split(' ', $_, 6);
 | 
|---|
| 28 |         # do not accept this line if less then six fields
 | 
|---|
| 29 |         next unless $_[5];
 | 
|---|
| 30 |         # if you have A LOT of clients you may speed things up by
 | 
|---|
| 31 |         # checking pid - no need to look further if this pid was already
 | 
|---|
| 32 |         # seen;  left as an exercise :-)
 | 
|---|
| 33 |         $client = $_[4];
 | 
|---|
| 34 |         next unless $client =~ /^\w+\./;       # expect 'dot' in a client name
 | 
|---|
| 35 |         next if grep($_ eq $client, @clients); # we want this name once
 | 
|---|
| 36 |         push(@clients, $client);
 | 
|---|
| 37 |     }
 | 
|---|
| 38 |     close(PCLIST);
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | if (-t) {
 | 
|---|
| 42 |     print <<'EOT';
 | 
|---|
| 43 | 
 | 
|---|
| 44 | Enter message for Samba clients of this host
 | 
|---|
| 45 | (terminated with single '.' or end of file):
 | 
|---|
| 46 | EOT
 | 
|---|
| 47 | 
 | 
|---|
| 48 |     while (<>) {
 | 
|---|
| 49 |         last if /^\.$/;
 | 
|---|
| 50 |         push(@message, $_);
 | 
|---|
| 51 |     }
 | 
|---|
| 52 | }
 | 
|---|
| 53 | else { # keep quiet and read message from stdin
 | 
|---|
| 54 |     @message = <>;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | foreach(@clients) {
 | 
|---|
| 58 | ##    print "To $_:\n";
 | 
|---|
| 59 |     if (open(SENDMSG,"|$smbshout $_")) {
 | 
|---|
| 60 |         print SENDMSG @message;
 | 
|---|
| 61 |         close(SENDMSG);
 | 
|---|
| 62 |     }
 | 
|---|
| 63 |     else {
 | 
|---|
| 64 |         warn "Cannot notify $_ with $smbshout:\n$!\n";
 | 
|---|
| 65 |     }
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | exit 0;
 | 
|---|
| 69 | 
 | 
|---|