3 # Prints info on all smb responding machines on a subnet.
4 # This script needs to be run on a machine without nmbd running and be
5 # run as root to get correct info from WIN95 clients.
8 # findsmb [subnet broadcast address]
10 # with no agrument it will list machines on the current subnet
12 # There will be a "+" in front of the workgroup name for machines that are
13 # local master browsers for that workgroup. There will be an "*" in front
14 # of the workgroup name for machines that are the domain master browser for
18 $SAMBABIN = "/usr/samba/bin";
20 for ($i = 0; $i < 2; $i++) { # test for -d option and broadcast address
31 sub ipsort
# do numeric sort on last field of IP address
38 # look for all machines that respond to a name lookup
40 open(NMBLOOKUP
,"$SAMBABIN/nmblookup $BCAST '*'|") ||
41 die("Can't run nmblookup '*'.\n");
43 # get rid of all lines that are not a response IP address,
44 # strip everything but IP address and sort by last field in address
46 @ipaddrs = sort ipsort
grep(s/ \*<00>.*$//,<NMBLOOKUP
>);
50 print "\nIP ADDR NETBIOS NAME WORKGROUP/OS/VERSION $BCAST\n";
51 print "---------------------------------------------------------------------\n";
53 foreach $ip (@ipaddrs) # loop through each IP address found
55 $ip =~ s/\n//; # strip newline from IP address
57 # find the netbios names registered by each machine
59 open(NMBLOOKUP
,"$SAMBABIN/nmblookup -r -A $ip|") ||
60 die("Can't get nmb name list.\n");
61 @nmblookup = <NMBLOOKUP
>;
64 # get the first <00> name
66 @name = grep(/<00> - /,@nmblookup);
69 # try without the -r option
70 open(NMBLOOKUP
,"$SAMBABIN/nmblookup -A $ip|") ||
71 die("Can't get nmb name list.\n");
72 @nmblookup = <NMBLOOKUP
>;
74 @name = grep(/<00> - /,@nmblookup);
77 if ($_) { # we have a netbios name
78 if (/GROUP/) { # is it a group name
79 ($name, $aliases, $type, $length, @addresses) =
80 gethostbyaddr(pack('C4',split('\.',$ip)),2);
81 if (! $name) { # could not get name
82 $name = "unknown nis name";
89 # do an smbclient command on the netbios name.
91 open(SMB
,"$SAMBABIN/smbclient -N -L '$name' -I $ip -U% |") ||
92 die("Can't do smbclient command.\n");
96 if ($DEBUG) { # if -d flag print results of nmblookup and smbclient
97 print "===============================================================\n";
102 # look for the OS= string
104 @info = grep(/OS=/,@smb);
106 if ($_) { # we found response
107 s/.*Domain=|OS=|Server=|\n//g; # strip out descriptions to make line shorter
109 } else { # no OS= string in response (WIN95 client)
111 # for WIN95 clients get workgroup name from nmblookup response
112 @name = grep(/<00> - <GROUP>/,@nmblookup);
118 $_ = "Unknown Workgroup";
122 # see if machine registered a local master browser name
123 if (grep(/<1d>/,@nmblookup)) {
124 $master = '+'; # indicate local master browser
125 if (grep(/<1b>/,@nmblookup)) { # how about domain master browser?
126 $master = '*'; # indicate domain master browser
129 $master = ' '; # not a browse master
132 # line up info in 3 columns
134 print "$ip".' 'x
(16-length($ip))."$name".' 'x
(14-length($name))."$master"."$_\n";
136 } else { # no netbios name found
137 # try getting the host name
138 ($name, $aliases, $type, $length, @addresses) =
139 gethostbyaddr(pack('C4',split('\.',$ip)),2);
140 if (! $name) { # could not get name
141 $name = "unknown nis name";
143 if ($DEBUG) { # if -d flag print results of nmblookup
144 print "===============================================================\n";
147 print "$ip".' 'x
(16-length($ip))."$name\n";