find all smb (windows, linux+samba) host in network and print disk shares

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$PDC='armada';
my %hosts=getComp($PDC);
foreach (keys %hosts)
{
print "="x50;
print "\n$_ =\> ".$hosts{$_}."\n";
print "="x50;
print "\n";
print join "\n",getShares($_);
print "\n";
print "-"x50;
print "\n\n";
}
#getShares('pandora');
sub getComp
{
my $dc=shift;
my %hosts;
my $result=qx~/usr/local/samba/bin/smbclient -g -N -L $dc 2> /dev/null~;
foreach (split(/\n/, $result))
{
if (/^Server/)
{
(/DLINK-PRN/)&&(next);
my (undef,$key, $value) = split(/\|/);
$hosts{$key} = $value;
}
}
return %hosts;
}
sub getShares
{
my $host=shift;
my $result=qx~/usr/local/samba/bin/smbclient -g -N -L $host -U 'user_g%user_g' 2
> /dev/null~;
my @final;
my @lines=split /\n/,$result;
#my @lines=grep {s/^\s+//} split /\n/,$result;
foreach (@lines)
{
if ((/^Disk/)&&($_!~/\$/))
{
my (undef,$name,$comment)=split (/\|/);
($comment ne '')&&($comment=" ($comment)");
push @final,($name.$comment);
}
}
return @final
}