#! /usr/bin/perl # anders@aftenposten.no # 2008-04-06 # Check if a samba share works: if smbclient can talk to Samba, and if the # share is available use Getopt::Std; $smbclient="/usr/local/bin/smbclient"; getopt('s:n:H:u:p:w:'); %shares=(); sub usage { print "Usage: check_smbshare -H -n -u \n"; print "-p -w [ -s ]\n"; exit(2); } if (!$opt_H) { print "ERROR: No hostname specified, use -H.\n\n"; usage; } if (!$opt_n) { print "ERROR: No NetBIOS hostname specified, use -n.\n\n"; usage; } if (!$opt_u) { print "ERROR: No username specified, use -u.\n\n"; usage; } if (!$opt_p) { print "ERROR: No password specified, use -p.\n\n"; usage; } if (!$opt_w) { print "ERROR: No workgroup specified, use -w.\n\n"; usage; } open(SMB, "$smbclient //$opt_n/ $opt_p -L $opt_n -I $opt_H -U $opt_u -W $opt_w 2>/dev/null |"); $phase = 0; while () { $txt = $_; $txt =~ s@^\s+@@; $txt =~ s@\s{2,}@\t@g; @f = split(/\t/, $txt); if ($phase == 0 && $f[0] =~ /^Sharename$/) { $phase = 1; } elsif ($phase == 1 && $f[0] !~ /^-{2,}$/) { if ($f[2]) { $shares{"$f[0]"} = 1; } else { $phase = 2; } } } close(SMB); $ret=$?; #print "Res: res1=$res1 res2=$res2\n"; if ($ret != 0) { print "FAIL: Smbclient returns errors.\n"; exit(2); } elsif ((!$opt_s)) { print "OK: Server $opt_H ($opt_n) has these shares: "; foreach $share (keys %shares) { print $share . " "; } print "\n"; } elsif (!exists $shares{"$opt_s"}) { print "FAIL: Share $opt_s not found on server $opt_H ($opt_n).\n"; exit(2); } else { print "OK: Share $opt_s exists on server $opt_H ($opt_n).\n"; exit(0); }