line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sys::Hostname::Long; |
2
|
1
|
|
|
1
|
|
12490
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
3
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
67
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Exporter; |
6
|
1
|
|
|
1
|
|
1593
|
use Sys::Hostname; |
|
1
|
|
|
|
|
1925
|
|
|
1
|
|
|
|
|
162
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Use perl < 5.6 compatible methods for now, change to 'use base' soon |
9
|
|
|
|
|
|
|
@Sys::Hostname::Long::ISA = qw/ Exporter Sys::Hostname /; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Use perl < 5.6 compatible methods for now, change to 'our' soon. |
12
|
1
|
|
|
1
|
|
7
|
use vars qw(@EXPORT $VERSION $hostlong %dispatch $lastdispatch); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
3579
|
|
13
|
|
|
|
|
|
|
@EXPORT = qw/ hostname_long /; |
14
|
|
|
|
|
|
|
$VERSION = '1.4'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
%dispatch = ( |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
'gethostbyname' => { |
19
|
|
|
|
|
|
|
'title' => 'Get Host by Name', |
20
|
|
|
|
|
|
|
'description' => '', |
21
|
|
|
|
|
|
|
'exec' => sub { |
22
|
|
|
|
|
|
|
return gethostbyname('localhost'); |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
'exec_hostname' => { |
27
|
|
|
|
|
|
|
'title' => 'Execute "hostname"', |
28
|
|
|
|
|
|
|
'description' => '', |
29
|
|
|
|
|
|
|
'exec' => sub { |
30
|
|
|
|
|
|
|
my $tmp = `hostname`; |
31
|
|
|
|
|
|
|
$tmp =~ tr/\0\r\n//d; |
32
|
|
|
|
|
|
|
return $tmp; |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
'win32_registry1' => { |
37
|
|
|
|
|
|
|
'title' => 'WIN32 Registry', |
38
|
|
|
|
|
|
|
'description' => 'LMachine/System/CurrentControlSet/Service/VxD/MSTCP/Domain', |
39
|
|
|
|
|
|
|
'exec' => sub { |
40
|
|
|
|
|
|
|
return eval q{ |
41
|
|
|
|
|
|
|
use Win32::TieRegistry ( TiedHash => '%RegistryHash' ); |
42
|
|
|
|
|
|
|
$RegistryHash{'LMachine'}{'System'}{'CurrentControlSet'}{'Services'}{'VxD'}{'MSTCP'}{'Domain'}; |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
'uname' => { |
48
|
|
|
|
|
|
|
'title' => 'POSIX::unae', |
49
|
|
|
|
|
|
|
'description' => '', |
50
|
|
|
|
|
|
|
'exec' => sub { |
51
|
|
|
|
|
|
|
return eval { |
52
|
|
|
|
|
|
|
local $SIG{__DIE__}; |
53
|
|
|
|
|
|
|
require POSIX; |
54
|
|
|
|
|
|
|
(POSIX::uname())[1]; |
55
|
|
|
|
|
|
|
}; |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# XXX This is the same as above - what happened to the other one !!! |
60
|
|
|
|
|
|
|
'win32_registry2' => { |
61
|
|
|
|
|
|
|
'title' => 'WIN32 Registry', |
62
|
|
|
|
|
|
|
'description' => 'LMachine/System/CurrentControlSet/Services/VxD/MSTCP/Domain', |
63
|
|
|
|
|
|
|
'exec' => sub { |
64
|
|
|
|
|
|
|
return eval q{ |
65
|
|
|
|
|
|
|
use Win32::TieRegistry ( TiedHash => '%RegistryHash' ); |
66
|
|
|
|
|
|
|
$RegistryHash{'LMachine'}{'System'}{'CurrentControlSet'}{'Services'}{'VxD'}{'MSTCP'}{'Domain'}; |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
}, |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
'exec_hostname_fqdn' => { |
72
|
|
|
|
|
|
|
'title' => 'Execute "hostname --fqdn"', |
73
|
|
|
|
|
|
|
'description' => '', |
74
|
|
|
|
|
|
|
'exec' => sub { |
75
|
|
|
|
|
|
|
# Skip for Solaris, and only run as non-root |
76
|
|
|
|
|
|
|
my $tmp; |
77
|
|
|
|
|
|
|
if ($< == 0) { |
78
|
|
|
|
|
|
|
$tmp = `su nobody -c "hostname --fqdn"`; |
79
|
|
|
|
|
|
|
} else { |
80
|
|
|
|
|
|
|
$tmp = `hostname --fqdn`; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
$tmp =~ tr/\0\r\n//d; |
83
|
|
|
|
|
|
|
return $tmp; |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
}, |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
'exec_hostname_domainname' => { |
88
|
|
|
|
|
|
|
'title' => 'Execute "hostname" and "domainname"', |
89
|
|
|
|
|
|
|
'description' => '', |
90
|
|
|
|
|
|
|
'exec' => sub { |
91
|
|
|
|
|
|
|
my $tmp = `hostname` . '.' . `domainname`; |
92
|
|
|
|
|
|
|
$tmp =~ tr/\0\r\n//d; |
93
|
|
|
|
|
|
|
return $tmp; |
94
|
|
|
|
|
|
|
}, |
95
|
|
|
|
|
|
|
}, |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
'network' => { |
99
|
|
|
|
|
|
|
'title' => 'Network Socket hostname (not DNS)', |
100
|
|
|
|
|
|
|
'description' => '', |
101
|
|
|
|
|
|
|
'exec' => sub { |
102
|
|
|
|
|
|
|
return eval q{ |
103
|
|
|
|
|
|
|
use IO::Socket; |
104
|
|
|
|
|
|
|
my $s = IO::Socket::INET->new( |
105
|
|
|
|
|
|
|
# m.root-servers.net (a remote IP number) |
106
|
|
|
|
|
|
|
PeerAddr => '202.12.27.33', |
107
|
|
|
|
|
|
|
# random safe port |
108
|
|
|
|
|
|
|
PeerPort => 2000, |
109
|
|
|
|
|
|
|
# We don't actually want to connect |
110
|
|
|
|
|
|
|
Proto => 'udp', |
111
|
|
|
|
|
|
|
) or die "Faile socket - $!"; |
112
|
|
|
|
|
|
|
gethostbyaddr($s->sockaddr(), AF_INET); |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
}, |
115
|
|
|
|
|
|
|
}, |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
'ip' => { |
118
|
|
|
|
|
|
|
'title' => 'Network Socket IP then Hostname via DNS', |
119
|
|
|
|
|
|
|
'description' => '', |
120
|
|
|
|
|
|
|
'exec' => sub { |
121
|
|
|
|
|
|
|
return eval q{ |
122
|
|
|
|
|
|
|
use IO::Socket; |
123
|
|
|
|
|
|
|
my $s = IO::Socket::INET->new( |
124
|
|
|
|
|
|
|
# m.root-servers.net (a remote IP number) |
125
|
|
|
|
|
|
|
PeerAddr => '202.12.27.33', |
126
|
|
|
|
|
|
|
# random safe port |
127
|
|
|
|
|
|
|
PeerPort => 2000, |
128
|
|
|
|
|
|
|
# We don't actually want to connect |
129
|
|
|
|
|
|
|
Proto => 'udp', |
130
|
|
|
|
|
|
|
) or die "Faile socket - $!"; |
131
|
|
|
|
|
|
|
$s->sockhost; |
132
|
|
|
|
|
|
|
}; |
133
|
|
|
|
|
|
|
}, |
134
|
|
|
|
|
|
|
}, |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# Dispatch from table |
139
|
|
|
|
|
|
|
sub dispatcher { |
140
|
2
|
|
|
2
|
0
|
6
|
my ($method, @rest) = @_; |
141
|
2
|
|
|
|
|
5
|
$lastdispatch = $method; |
142
|
2
|
|
|
|
|
12
|
return $dispatch{$method}{exec}(@rest); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub dispatch_keys { |
146
|
0
|
|
|
0
|
0
|
0
|
return sort keys %dispatch; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub dispatch_title { |
150
|
0
|
|
|
0
|
0
|
0
|
return $dispatch{$_[0]}{title}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub dispatch_description { |
154
|
0
|
|
|
0
|
0
|
0
|
return $dispatch{$_[0]}{description}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub hostname_long { |
158
|
1
|
50
|
|
1
|
0
|
9
|
return $hostlong if defined $hostlong; # Cached copy (takes a while to lookup sometimes) |
159
|
1
|
|
|
|
|
3
|
my ($ip, $debug) = @_; |
160
|
|
|
|
|
|
|
|
161
|
1
|
|
|
|
|
5
|
$hostlong = dispatcher('uname'); |
162
|
|
|
|
|
|
|
|
163
|
1
|
50
|
|
|
|
9
|
unless ($hostlong =~ m|.*\..*|) { |
164
|
1
|
50
|
|
|
|
20
|
if ($^O eq 'MacOS') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# http://bumppo.net/lists/macperl/1999/03/msg00282.html |
166
|
|
|
|
|
|
|
# suggests that it will work (checking localhost) on both |
167
|
|
|
|
|
|
|
# Mac and Windows. |
168
|
|
|
|
|
|
|
# Personally this makes no sense what so ever as |
169
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('gethostbyname'); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
} elsif ($^O eq 'IRIX') { # XXX Patter match string ! |
172
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('exec_hostname'); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
} elsif ($^O eq 'cygwin') { |
175
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('win32_registry1'); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
} elsif ($^O eq 'MSWin32') { |
178
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('win32_registry2'); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} elsif ($^O =~ m/(bsd|nto)/i) { |
181
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('exec_hostname'); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# (covered above) } elsif ($^O eq "darwin") { |
184
|
|
|
|
|
|
|
# $hostlong = dispatcher('uname'); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
} elsif ($^O eq 'solaris') { |
187
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('exec_hostname_domainname'); |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
} else { |
190
|
1
|
|
|
|
|
4
|
$hostlong = dispatcher('exec_hostname_fqdn'); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
1
|
50
|
33
|
|
|
28
|
if (!defined($hostlong) || $hostlong eq "") { |
194
|
|
|
|
|
|
|
# FALL BACK - Requires working internet and DNS and reverse |
195
|
|
|
|
|
|
|
# lookups of your IP number. |
196
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('network'); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
1
|
50
|
33
|
|
|
25
|
if ($ip && !defined($hostlong) || $hostlong eq "") { |
|
|
|
33
|
|
|
|
|
200
|
0
|
|
|
|
|
0
|
$hostlong = dispatcher('ip'); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
1
|
50
|
|
|
|
619
|
warn "Sys::Hostname::Long - Last Dispatch method = $lastdispatch" if ($debug); |
204
|
1
|
|
|
|
|
24
|
return $hostlong; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
1; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
__END__ |