Revised smb-wall (Was: [Samba] smbclient -M --> ERRmsgoff?)

Wilkinson Charlie E Charlie.E.Wilkinson at irs.gov
Fri Apr 14 20:45:55 GMT 2006


I've gotten smb-wall to work with Samba v3.  If there's a better way,
please let me know.  If not, what's the chances this could be included
in a future release?

#!/usr/bin/perl
#
#@(#) smb-wall.pl Description:
#@(#) A perl script which allows you to announce whatever you choose to
#@(#) every PC client currently connected to a Samba Server...
#@(#) ...using "smbclient -M" message to winpopup service.
#@(#) Default usage is to message every connected PC.
#@(#) Alternate usage is to message every pc on the argument list.
#@(#)  Hacked up by Keith Farrar <farrar at parc.xerox.com>
#
# Cleanup and corrections by
# Michal Jaegermann <michal at ellpspace.math.ualberta.ca>
# Message to send can be now also fed (quietly) from stdin; a pipe will
do.

# Modified 20060414 to work with Samba v3 by
# Charlie Wilkinson <cwilkins at boinklabs.com>
# Added call to nmblookup to resolve IP addresses to NetBIOS names
#=======================================================================
======

$smbstatus = "/usr/bin/smbstatus -S";
$smbshout = "/usr/bin/smbclient -M";
$nmblookup = "/usr/bin/nmblookup -A";

if (@ARGV) {
    @clients = @ARGV;
    undef @ARGV;
}
else {  # no clients specified explicitly
    open(PCLIST, "$smbstatus |") || die "$smbstatus failed!.\n$!\n";
    while(<PCLIST>) {
        last if /^Locked files:/;
        split(' ', $_, 6);
        # do not accept this line if less then six fields
        next unless $_[5];
        # if you have A LOT of clients you may speed things up by
        # checking pid - no need to look further if this pid was already
        # seen;  left as an exercise :-)
        $client = $_[2];
        if( $client =~ /^\d+\./ ){      # Lookup NetBIOS names for IP's
            open(NMBOUT, "$nmblookup $client |") || die "$nmblookup
failed!.\n$!\n";
            while($line=<NMBOUT>) {
                if( $line =~ /^\s*(\S+)\s<00> -\s*M/ ){
                    $client = $1;
                    last;
                }
            }
            close(NMBOUT);
        }
        $client =~ tr/a-z/A-Z/;                 # to better catch/reject
dupes
        next if $client =~ /^\d+\./;            # toss failed lookup (PC
down?)
        next if grep($_ eq $client, @clients);  # we want this name once
        push(@clients, $client);
    }
    close(PCLIST);
}

# debugging...
#print "@clients\n";
#exit(0);

if (-t) {
    print <<'EOT';

Enter message for Samba clients of this host
(terminated with single '.' or end of file):
EOT

    while (<>) {
        last if /^\.$/;
        push(@message, $_);
    }
}
else { # keep quiet and read message from stdin
    @message = <>;
}

foreach(@clients) {
##    print "To $_:\n";
    if (open(SENDMSG,"|$smbshout $_")) {
        print SENDMSG @message;
        close(SENDMSG);
    }
    else {
        warn "Cannot notify $_ with $smbshout:\n$!\n";
    }
}

exit 0;


More information about the samba mailing list