first public release of samba4 code
[samba.git] / source / 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 sub ipsort                      # do numeric sort on last field of IP address
39 {
40   @t1 = split(/\./,$a);
41   @t2 = split(/\./,$b);
42   @t1[3] <=> @t2[3];
43 }
44
45 # look for all machines that respond to a name lookup
46
47 open(NMBLOOKUP,"$SAMBABIN/nmblookup $BCAST '*'|") || 
48   die("Can't run nmblookup '*'.\n");
49
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
52
53 @ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);
54
55 # print header info
56
57 print "\nIP ADDR         NETBIOS NAME   WORKGROUP/OS/VERSION $BCAST\n";
58 print "---------------------------------------------------------------------\n";
59
60 foreach $ip (@ipaddrs)          # loop through each IP address found
61 {
62   $ip =~ s/\n//;                # strip newline from IP address
63
64 # find the netbios names registered by each machine
65
66   open(NMBLOOKUP,"$SAMBABIN/nmblookup $R_OPTION -A $ip|") || 
67         die("Can't get nmb name list.\n");
68   @nmblookup = <NMBLOOKUP>;
69   close NMBLOOKUP;
70
71 # get the first <00> name
72
73   @name = grep(/<00>/,@nmblookup);
74   $_ = @name[0];
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";
81         }
82     } else {
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+/;
87         $name = $1;
88     }
89
90 # do an smbclient command on the netbios name.
91
92     open(SMB,"$SAMBABIN/smbclient -N -L $name -I $ip -U% |") ||
93         die("Can't do smbclient command.\n");
94     @smb = <SMB>;
95     close SMB;
96
97     if ($DEBUG) {               # if -d flag print results of nmblookup and smbclient
98       print "===============================================================\n";
99       print @nmblookup;
100       print @smb;
101     }
102
103 # look for the OS= string
104
105     @info = grep(/OS=/,@smb);
106     $_ = @info[0];
107     if ($_) {                           # we found response
108       s/Domain=|OS=|Server=|\n//g;      # strip out descriptions to make line shorter
109
110     } else {                            # no OS= string in response (WIN95 client)
111
112 # for WIN95 clients get workgroup name from nmblookup response
113       @name = grep(/<00> - <GROUP>/,@nmblookup);
114       $_ = @name[0];
115       if ($_) {
116 # Same as before for space and characters
117         /(.{1,15})\s+<00>\s+/;
118         $_ = "[$1]";
119       } else {
120         $_ = "Unknown Workgroup";
121       }
122     }
123
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
129       }
130     } else {
131       $master = ' ';                    # not a browse master
132     }
133
134 # line up info in 3 columns
135
136     print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
137
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";
144     }
145     if ($DEBUG) {                       # if -d flag print results of nmblookup
146       print "===============================================================\n";
147       print @nmblookup;
148     }
149     print "$ip".' 'x(16-length($ip))."$name\n";
150   }
151
152