-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSambaHost.pl
More file actions
81 lines (73 loc) · 1.47 KB
/
getSambaHost.pl
File metadata and controls
81 lines (73 loc) · 1.47 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl -w
use Getopt::Std;
$ENV{'PATH'}='/bin:/sbin:/usr/bin:/usr/sbin:/usr/opt/networker/bin:/usr/local/bin:/usr/sfw/bin:/opt/samba/bin';
getopts('p:');
$dns = "10.10.10.30";
if (!$opt_p) {
print "usage: getSambaHost.pl -p process_id\n";
exit(1);
}
$process = $opt_p;
if (&itsProcess($process) == 1) {
if (($ip=itsItSamba($process)) ne "0") {
$os = `uname`;
if ($os =~ /SunOS/) {
print (getSunHost($ip));
print "\n";
}
elsif ($os =~ /HP-UX/) {
print (getHpHost($ip));
print "\n";
}
else {
print "Unsupported Operatin System: $os\n";
exit(1);
}
}
else {
print "$process is not a samba process\n";
exit(1);
}
}
else {
print "$opt_p is not a process\n";
exit(1);
}
sub getHpHost {
$ip = shift;
if(($lookUp = `nslookup $ip $dns`) ne '\0') {
@lines = split(/\s+/, $lookUp);
return $lines[-3];
}
return 0;
}
sub getSunHost {
$ip = shift;
open(NAME, "nslookup $ip $dns|") or die "$!\n";
while (<NAME>) {
next if $_ !~ /in-addr.arpa/;
@line = split(/\s+/, $_);
chop($line[-1]);
return $line[-1];
}
}
sub itsItSamba {
$proc = shift;
@getProc = `smbstatus 2>/dev/null`;
foreach (@getProc) {
next if $_ !~ /$proc/;
@line = split(/\s+/, $_);
return $line[2];
}
return "0";
}
sub itsProcess {
$proc = shift;
open(PS, "ps -ef|") or die "Can't execute: $!";
while (<PS>) {
if ($_ =~ /$proc/ && $_ !~ /$0/) {
return 1;
}
}
return 0;
}