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 = "@BINDIR@";
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>);
62 print "IP ADDR NETBIOS NAME WORKGROUP/OS/VERSION $BCAST\n";
63 print "---------------------------------------------------------------------\n";
65 foreach $ip (@ipaddrs) # loop through each IP address found
67 $ip =~ s/\n//; # strip newline from IP address
69 # find the netbios names registered by each machine
71 open(NMBLOOKUP,"$SAMBABIN/nmblookup $R_OPTION -A $ip --debuglevel=0|") ||
72 die("Can't get nmb name list.\n");
73 @nmblookup = <NMBLOOKUP>;
76 # get the first <00> name
78 @name = grep(/<00>/,@nmblookup);
81 if ($_) { # we have a netbios name
82 if (/GROUP/) { # is it a group name
83 ($name, $aliases, $type, $length, @addresses) =
84 gethostbyaddr(pack('C4',split('\.',$ip)),2);
85 if (! $name) { # could not get name
86 $name = "unknown nis name";
89 # The Netbios name can contain lot of characters also '<' '>'
90 # and spaces. The following cure inside name space but not
91 # names starting or ending with spaces
92 /(.{1,15})\s+<00>\s+/;
97 # do an smbclient command on the netbios name.
100 open(SMB,"$SAMBABIN/smbclient -L $name -I $ip -N --debuglevel=1 2>&1 |") ||
101 die("Can't do smbclient command.\n");
105 if ($DEBUG) { # if -d flag print results of nmblookup and smbclient
106 print "===============================================================\n";
111 # look for the OS= string
113 @info = grep(/OS=/,@smb);
115 if ($_) { # we found response
116 s/Domain=|OS=|Server=|\n//g; # strip out descriptions to make line shorter
118 } else { # no OS= string in response (WIN95 client)
120 # for WIN95 clients get workgroup name from nmblookup response
121 @name = grep(/<00> - <GROUP>/,@nmblookup);
124 # Same as before for space and characters
125 /(.{1,15})\s+<00>\s+/;
128 $_ = "Unknown Workgroup";
133 # see if machine registered a local master browser name
134 if (grep(/<1d>/,@nmblookup)) {
135 $master = '+'; # indicate local master browser
136 if (grep(/<1b>/,@nmblookup)) { # how about domain master browser?
137 $master = '*'; # indicate domain master browser
140 $master = ' '; # not a browse master
143 # line up info in 3 columns
145 print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
147 } else { # no netbios name found
148 # try getting the host name
149 ($name, $aliases, $type, $length, @addresses) =
150 gethostbyaddr(pack('C4',split('\.',$ip)),2);
151 if (! $name) { # could not get name
152 $name = "unknown nis name";
154 if ($DEBUG) { # if -d flag print results of nmblookup
155 print "===============================================================\n";
158 print "$ip".' 'x(16-length($ip))."$name\n";