#!/usr/bin/perl
#
# Test SMB Connections using smbclient, you'll need Samba
#  Just tries to get a list of shares from the box
#
# For use with "mon".
#
# smb.monitor -p password -u username host1 [ hostN... ]
#
#    Copyright (C) 2005, David Busby
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
exit 0 if (
!@ARGV);
use Getopt::Std;
getopts ("p:u:");
# Samba Default
#my $resolve_order = "lmhosts hosts wins bcast"
my $resolve_order = 'wins bcast';
my $debug_level = 1;
my %bad;
# Check the hosts
foreach my $host (@ARGV)
{
  my $res = do
  {
    my $x;
    my $cmd = "/usr/bin/smbclient -d $debug_level -L $host -R '$resolve_order' -U '$opt_u\%$opt_p' 2>&1";
    my $buf = `$cmd`;
    $x->{status} = $?;
    $x->{buffer} = $buf;
    $x;
  };
  if ($res->{status}!=0)
  {
    $bad{$host} = $res;
  }
}
# Write messages about the failures
foreach my $h (keys %bad)
{
  print "SMB FAILURE: $h $bad{$h}->{status} - $bad{$h}->{buffer}\n";
}
exit scalar(keys(%bad));