sync 3.0 into HEAD for the last time
[vlendec/samba-autobuild/.git] / source3 / script / findsmb.in
1 #!@PERL@
2 #
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.
6 #
7 # syntax:
8 #    findsmb [-d|-D] [-r] [subnet broadcast address]
9 #
10 # with no agrument it will list machines on the current subnet
11 #
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
15 # that workgroup.
16
17 # Options:
18 #
19 # -d|-D         enable debug
20 # -r            add -r option to nmblookup when finding netbios name
21 #
22
23 $SAMBABIN = "@prefix@/bin";
24
25 for ($i = 0; $i < 2; $i++) {    # test for -d and -r options
26         $_ = shift;
27         if (m/-d|-D/) {
28                 $DEBUG = 1;
29         } elsif (m/-r/) {
30                 $R_OPTION = "-r";
31         }
32 }
33
34 if ($_) {                       # set broadcast address if it was specified
35         $BCAST = "-B $_";
36 }
37
38
39 ######################################################################
40 # do numeric sort on last field of IP address
41 sub ipsort                      
42 {
43         @t1 = split(/\./,$a);
44         @t2 = split(/\./,$b);
45         @t1[3] <=> @t2[3];
46 }
47 ######################################################################
48
49 # look for all machines that respond to a name lookup
50
51 open(NMBLOOKUP,"$SAMBABIN/nmblookup $BCAST '*' --debuglevel=0|") || 
52         die("Can't run nmblookup '*'.\n");
53
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
56
57 @ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);
58
59 # print header info
60
61 print "\nIP ADDR         NETBIOS NAME   WORKGROUP/OS/VERSION $BCAST\n";
62 print "---------------------------------------------------------------------\n";
63
64 foreach $ip (@ipaddrs)          # loop through each IP address found
65 {
66         $ip =~ s/\n//;          # strip newline from IP address
67
68         # find the netbios names registered by each machine
69
70         open(NMBLOOKUP,"$SAMBABIN/nmblookup $R_OPTION -A $ip --debuglevel=0|") || 
71                 die("Can't get nmb name list.\n");
72         @nmblookup = <NMBLOOKUP>;
73         close NMBLOOKUP;
74
75         # get the first <00> name
76
77         @name = grep(/<00>/,@nmblookup);
78         $_ = @name[0];
79
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";
86                         }
87                 } else {
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+/;
92                         $name = $1;
93                         $name =~ s/^\s+//g;
94                 }
95
96                 # do an smbclient command on the netbios name.
97
98                 if ( "$name" ) {
99                         open(SMB,"$SAMBABIN/smbclient -L $name -I $ip -N --debuglevel=1 2>&1 |") ||
100                                 die("Can't do smbclient command.\n");
101                         @smb = <SMB>;
102                         close SMB;
103
104                         if ($DEBUG) {           # if -d flag print results of nmblookup and smbclient
105                                 print "===============================================================\n";
106                                 print @nmblookup;
107                                 print @smb;
108                         }
109
110                         # look for the OS= string
111
112                         @info = grep(/OS=/,@smb);
113                         $_ = @info[0];
114                         if ($_) {                               # we found response
115                                 s/Domain=|OS=|Server=|\n//g;    # strip out descriptions to make line shorter
116
117                         } else {                                # no OS= string in response (WIN95 client)
118
119                                 # for WIN95 clients get workgroup name from nmblookup response
120                                 @name = grep(/<00> - <GROUP>/,@nmblookup);
121                                 $_ = @name[0];
122                                 if ($_) {
123                                         # Same as before for space and characters
124                                         /(.{1,15})\s+<00>\s+/;
125                                         $_ = "[$1]";
126                                 } else {
127                                         $_ = "Unknown Workgroup";
128                                 }
129                         }
130                 }
131
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
137                         }
138                 } else {
139                         $master = ' ';                  # not a browse master
140                 }
141
142                 # line up info in 3 columns
143
144                 print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
145
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";
152                 }
153                 if ($DEBUG) {                   # if -d flag print results of nmblookup
154                         print "===============================================================\n";
155                         print @nmblookup;
156                 }
157                 print "$ip".' 'x(16-length($ip))."$name\n";
158         }
159
160