82aeb28dab31d96f223a83e7f72ec8e3981b3a78
[samba.git] / packaging / SGI / findsmb
1 #!/bin/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 [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
18 $SAMBABIN = "/usr/samba/bin";
19
20 for ($i = 0; $i < 2; $i++) {    # test for -d option and broadcast address
21   $_ = shift;
22   if (m/-d|-D/) {
23     $DEBUG = 1;
24   } else  {
25     if ($_) {
26       $BCAST = "-B $_";
27     }
28   }
29 }
30
31 sub ipsort                      # do numeric sort on last field of IP address
32 {
33   @t1 = split(/\./,$a);
34   @t2 = split(/\./,$b);
35   @t1[3] <=> @t2[3];
36 }
37
38 # look for all machines that respond to a name lookup
39
40 open(NMBLOOKUP,"$SAMBABIN/nmblookup $BCAST '*'|") || 
41   die("Can't run nmblookup '*'.\n");
42
43 # get rid of all lines that are not a response IP address,
44 # strip everything but IP address and sort by last field in address
45
46 @ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);
47
48 # print header info
49
50 print "\nIP ADDR         NETBIOS NAME   WORKGROUP/OS/VERSION $BCAST\n";
51 print "---------------------------------------------------------------------\n";
52
53 foreach $ip (@ipaddrs)          # loop through each IP address found
54 {
55   $ip =~ s/\n//;                # strip newline from IP address
56
57 # find the netbios names registered by each machine
58
59   open(NMBLOOKUP,"$SAMBABIN/nmblookup -r -A $ip|") || 
60         die("Can't get nmb name list.\n");
61   @nmblookup = <NMBLOOKUP>;
62   close NMBLOOKUP;
63
64 # get the first <00> name
65
66   @name = grep(/<00>/,@nmblookup);
67   $_ = @name[0];
68   if ($_) {                     # we have a netbios name
69     /(\S+)/;
70     $name = $1;
71
72 # do an smbclient command on the netbios name.
73
74     open(SMB,"$SAMBABIN/smbclient -N -L $name -I $ip -U% |") ||
75         die("Can't do smbclient command.\n");
76     @smb = <SMB>;
77     close SMB;
78
79     if ($DEBUG) {               # if -d flag print results of nmblookup and smbclient
80       print "===============================================================\n";
81       print @nmblookup;
82       print @smb;
83     }
84
85 # look for the OS= string
86
87     @info = grep(/OS=/,@smb);
88     $_ = @info[0];
89     if ($_) {                           # we found response
90       s/Domain=|OS=|Server=|\n//g;      # strip out descriptions to make line shorter
91
92     } else {                            # no OS= string in response (WIN95 client)
93
94 # for WIN95 clients get workgroup name from nmblookup response
95       @name = grep(/<00> - <GROUP>/,@nmblookup);
96       $_ = @name[0];
97       if ($_) {
98         /(\S+)/;
99         $_ = "[$1]";
100       } else {
101         $_ = "Unknown Workgroup";
102       }
103     }
104
105 # see if machine registered a local master browser name
106     if (grep(/<1d>/,@nmblookup)) {
107       $master = '+';                    # indicate local master browser
108       if (grep(/<1b>/,@nmblookup)) {    # how about domain master browser?
109         $master = '*';                  # indicate domain master browser
110       }
111     } else {
112       $master = ' ';                    # not a browse master
113     }
114
115 # line up info in 3 columns
116
117     print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
118
119   } else {                              # no netbios name found
120 # try getting the host name
121     ($name, $aliases, $type, $length, @addresses) = 
122       gethostbyaddr(pack('C4',split('\.',$ip)),2);
123     if (! $name) {                      # could not get name
124       $name = "unknown nis name";
125     }
126     if ($DEBUG) {                       # if -d flag print results of nmblookup
127       print "===============================================================\n";
128       print @nmblookup;
129     }
130     print "$ip".' 'x(16-length($ip))."$name\n";
131   }
132
133