Browsing, seeing all machines from Unix client

Herb Lewis herb at chomps.engr.sgi.com
Mon Mar 2 20:13:53 GMT 1998


Steven Grunza wrote:
> 
> Hello,
>   I would like to list all SMB hosts on my network.  Under Windows 95, the
> Network Neighborhood icon will provide this list.  Is there a similar
> method on the unix side?  I can run smbclient -L host and see all the
> available services but in order to get the name of the host, I need to use
> a Windows 95 PC.
> 
>   We are investigating using samba as our Unix printing solution and the
> ability to see who's on the network might prove to be a very usefull
> diagnostic tool.
> Thanks.

Here is a perl script I hacked together to list smb hosts on our
network. This script needs to be run on a machine without nmbd running
and be run as root to get correct info from WIN95 clients. Change the 
path for nmblookup and smbclient as needed. I plan on checking this
in on the packaging/SGI tree when I finish the documentation. I'm
just learning perl so this is probably not the best way of doing this
but it works.

-- 
======================================================================
Herb Lewis                                   Silicon Graphics 
Technical Marketing                          2011 N Shoreline Blvd
Network Systems Division                     Mountain View, CA  94043  
herb at sgi.com                                 Tel: 650-933-2177
http://www.sgi.com                           Fax: 650-932-2177          
======================================================================
-------------- next part --------------
#!/bin/perl
#
# Prints info on all smb responding machines on a subnet.
# This script needs to be run on a machine without nmbd running and be
# run as root to get correct info from WIN95 clients.
#
# syntax:
#    findsmb [subnet broadcast address]
#
# with no agrument it will list machines on the current subnet
#
# There will be a "+" in front of the workgroup name for machines that are
# local master browsers for that workgroup. There will be an "*" in front
# of the workgroup name for machines that are the domain master browser for
# that workgroup.
#

for ($i = 0; $i < 2; $i++) {
  $_ = shift;
  if (m/-d|-D/) {
    $DEBUG = 1;
  } else  {
    if ($_) {
      $BCAST = "-B $_";
    }
  }
}

sub ipsort
{
  @t1 = split(/\./,$a);
  @t2 = split(/\./,$b);
  @t1[3] <=> @t2[3];
}

open(NMBLOOKUP,"/usr/samba/bin/nmblookup $BCAST '*'|") || 
  die("Can't run nmblookup '*'.\n");

@ipaddrs = sort ipsort grep(s/ \*<00>.*$//,<NMBLOOKUP>);

print "\nIP ADDR         NETBIOS NAME   WORKGROUP/OS/VERSION $BCAST\n";
print "---------------------------------------------------------------------\n";
foreach $ip (@ipaddrs)
{
  $ip =~ s/\n//;
  open(NMBLOOKUP,"/usr/samba/bin/nmblookup -r -A $ip|") || 
	die("Can't get nmb name list.\n");
  @nmblookup = <NMBLOOKUP>;
  close NMBLOOKUP;
  @name = grep(/<00>/, at nmblookup);
  $_ = @name[0];
  if ($_) {
    /(\S+)/;
    $name = $1;
    open(SMB,"/usr/samba/bin/smbclient -N -L $name -I $ip -U% |");
    @smb = <SMB>;
    close SMB;
    if ($DEBUG) {
      print "===============================================================\n";
      print @nmblookup;
      print @smb;
    }
    @info = grep(/OS=/, at smb);
    $_ = @info[0];
    if ($_) {
      s/Domain=|OS=|Server=|\n//g;
    } else {
      @name = grep(/<00> - <GROUP>/, at nmblookup);
      $_ = @name[0];
      /(\S+)/;
      $_ = "[$1]";
    }
    if (grep(/<1d>/, at nmblookup)) {
      $master = '+';
      if (grep(/<1b>/, at nmblookup)) {
        $master = '*';
      }
    } else {
      $master = ' ';
    }
    print "$ip".' 'x(16-length($ip))."$name".' 'x(14-length($name))."$master"."$_\n";
  } else {
    ($name, $aliases, $type, $length, @addresses) = 
      gethostbyaddr(pack('C4',split('\.',$ip)),2);
    if (! $name) {
      $name = "unknown nis name";
    }
    if ($DEBUG) {
      print "===============================================================\n";
      print @nmblookup;
    }
    print "$ip".' 'x(16-length($ip))."$name\n";
  }
} 



More information about the samba mailing list