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
38 sub ipsort # do numeric sort on last field of IP address
45 # look for all machines that respond to a name lookup
47 open(NMBLOOKUP,"$SAMBABIN/nmblookup $BCAST '*'|") ||
48 die("Can't run nmblookup '*'.\n");
50 # get rid of all lines that are not a response IP address,
51 # strip everything but IP address and sort by last field in address
53 @ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);
57 print "\nIP ADDR NETBIOS NAME WORKGROUP/OS/VERSION $BCAST\n";
58 print "---------------------------------------------------------------------\n";
60 foreach $ip (@ipaddrs) # loop through each IP address found
62 $ip =~ s/\n//; # strip newline from IP address
64 # find the netbios names registered by each machine
66 open(NMBLOOKUP,"$SAMBABIN/nmblookup $R_OPTION -A $ip|") ||
67 die("Can't get nmb name list.\n");
68 @nmblookup = <NMBLOOKUP>;
71 # get the first <00> name
73 @name = grep(/<00>/,@nmblookup);
75 if ($_) { # we have a netbios name
76 if (/GROUP/) { # is it a group name
77 ($name, $aliases, $type, $length, @addresses) =
78 gethostbyaddr(pack('C4',split('\.',$ip)),2);
79 if (! $name) { # could not get name
80 $name = "unknown nis name";
83 # The Netbios name can contain lot of characters also '<' '>'
84 # and spaces. The follwing cure inside name space but not
85 # names starting or ending with spaces
86 /(.{1,15})\s+<00>\s+/;
90 # do an smbclient command on the netbios name.
92 open(SMB,"$SAMBABIN/smbclient -N -L $name -I $ip -U% |") ||
93 die("Can't do smbclient command.\n");
97 if ($DEBUG) { # if -d flag print results of nmblookup and smbclient
98 print "===============================================================\n";
103 # look for the OS= string
105 @info = grep(/OS=/,@smb);
107 if ($_) { # we found response
108 s/Domain=|OS=|Server=|\n//g; # strip out descriptions to make line shorter
110 } else { # no OS= string in response (WIN95 client)
112 # for WIN95 clients get workgroup name from nmblookup response
113 @name = grep(/<00> - <GROUP>/,@nmblookup);
116 # Same as before for space and characters
117 /(.{1,15})\s+<00>\s+/;
120 $_ = "Unknown Workgroup";
124 # see if machine registered a local master browser name
125 if (grep(/<1d>/,@nmblookup)) {
126 $master = '+'; # indicate local master browser
127 if (grep(/<1b>/,@nmblookup)) { # how about domain master browser?
128 $master = '*'; # indicate domain master browser
131 $master = ' '; # not a browse master
134 # line up info in 3 columns
136 print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
138 } else { # no netbios name found
139 # try getting the host name
140 ($name, $aliases, $type, $length, @addresses) =
141 gethostbyaddr(pack('C4',split('\.',$ip)),2);
142 if (! $name) { # could not get name
143 $name = "unknown nis name";
145 if ($DEBUG) { # if -d flag print results of nmblookup
146 print "===============================================================\n";
149 print "$ip".' 'x(16-length($ip))."$name\n";