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 [-d|-D] [-r] [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
20 # -r add -r option to nmblookup when finding netbios name
23 $SAMBABIN = "@prefix@/bin";
25 for ($i = 0; $i < 2; $i++) { # test for -d and -r options
34 if ($_) { # set broadcast address if it was specified
39 ######################################################################
40 # do numeric sort on last field of IP address
47 ######################################################################
49 # look for all machines that respond to a name lookup
51 open(NMBLOOKUP,"$SAMBABIN/nmblookup $BCAST '*' --debuglevel=0|") ||
52 die("Can't run nmblookup '*'.\n");
54 # get rid of all lines that are not a response IP address,
55 # strip everything but IP address and sort by last field in address
57 @ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);
61 print "\nIP ADDR NETBIOS NAME WORKGROUP/OS/VERSION $BCAST\n";
62 print "---------------------------------------------------------------------\n";
64 foreach $ip (@ipaddrs) # loop through each IP address found
66 $ip =~ s/\n//; # strip newline from IP address
68 # find the netbios names registered by each machine
70 open(NMBLOOKUP,"$SAMBABIN/nmblookup $R_OPTION -A $ip --debuglevel=0|") ||
71 die("Can't get nmb name list.\n");
72 @nmblookup = <NMBLOOKUP>;
75 # get the first <00> name
77 @name = grep(/<00>/,@nmblookup);
80 if ($_) { # we have a netbios name
81 if (/GROUP/) { # is it a group name
82 ($name, $aliases, $type, $length, @addresses) =
83 gethostbyaddr(pack('C4',split('\.',$ip)),2);
84 if (! $name) { # could not get name
85 $name = "unknown nis name";
88 # The Netbios name can contain lot of characters also '<' '>'
89 # and spaces. The follwing cure inside name space but not
90 # names starting or ending with spaces
91 /(.{1,15})\s+<00>\s+/;
96 # do an smbclient command on the netbios name.
99 open(SMB,"$SAMBABIN/smbclient -L $name -I $ip -N --debuglevel=1 2>&1 |") ||
100 die("Can't do smbclient command.\n");
104 if ($DEBUG) { # if -d flag print results of nmblookup and smbclient
105 print "===============================================================\n";
110 # look for the OS= string
112 @info = grep(/OS=/,@smb);
114 if ($_) { # we found response
115 s/Domain=|OS=|Server=|\n//g; # strip out descriptions to make line shorter
117 } else { # no OS= string in response (WIN95 client)
119 # for WIN95 clients get workgroup name from nmblookup response
120 @name = grep(/<00> - <GROUP>/,@nmblookup);
123 # Same as before for space and characters
124 /(.{1,15})\s+<00>\s+/;
127 $_ = "Unknown Workgroup";
132 # see if machine registered a local master browser name
133 if (grep(/<1d>/,@nmblookup)) {
134 $master = '+'; # indicate local master browser
135 if (grep(/<1b>/,@nmblookup)) { # how about domain master browser?
136 $master = '*'; # indicate domain master browser
139 $master = ' '; # not a browse master
142 # line up info in 3 columns
144 print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
146 } else { # no netbios name found
147 # try getting the host name
148 ($name, $aliases, $type, $length, @addresses) =
149 gethostbyaddr(pack('C4',split('\.',$ip)),2);
150 if (! $name) { # could not get name
151 $name = "unknown nis name";
153 if ($DEBUG) { # if -d flag print results of nmblookup
154 print "===============================================================\n";
157 print "$ip".' 'x(16-length($ip))."$name\n";