forked from sonick13/PerlProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetip
88 lines (76 loc) · 2.48 KB
/
getip
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
82
83
84
85
86
87
88
#!/usr/bin/env perl
#cito M:755 O:0 G:0 T:/usr/bin/getip
#--------------------------------------------------------------------------
# Project Name - PerlProjects/source/getip
# Started On - Wed 8 May 14:35:49 BST 2019
# Last Change - Mon 16 Nov 16:39:41 GMT 2020
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#--------------------------------------------------------------------------
# Dependencies:
#
# libnet-address-ip-local-perl (>= 0.1.2-3)
# libwww-perl (>= 6.15-1)
# perl (>= 5.22.1-9)
#------------------------------------------------------------------------------
use strict;
use warnings;
use autodie;
no warnings 'uninitialized';
my $CurVer = "2020-11-16";
my $Progrm = 'getip';
sub Usage {
print(qq{Usage: $Progrm [OPTS]
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
-I, --ip-only - Show only the IP address.
-e, --external - Omit the internal IP address.
-i, --internal - Omit the external IP address.
} =~ tr/\t//dr)
}
my $Internal = 'true';
my $External = 'true';
my $IntExtDetected = 0;
my $IntStr = 'Internal IP: ';
my $ExtStr = 'External IP: ';
while (defined($ARGV[0])) {
if ($ARGV[0] =~ '^(--help|-h|-\?)$') {
Usage(); exit(0)
} elsif ($ARGV[0] =~ '^(--version|-v)$') {
print("$CurVer\n"); exit(0)
} elsif ($ARGV[0] =~ '^(--internal|-i)$') {
$IntExtDetected++; $Internal = 'true'; $External = 'false'
} elsif ($ARGV[0] =~ '^(--external|-e)$') {
$IntExtDetected++; $Internal = 'false'; $External = 'true'
} elsif ($ARGV[0] =~ '^(--ip-only|-I)$') {
$IntStr = ''; $ExtStr = ''
} else {
die("Incorrect option(s) specified")
}
shift()
}
$IntExtDetected == 2 and die("Either '--internal|-i' or '--external|-e'");
if ($Internal eq 'true') {
use Net::Address::IP::Local;
my $Data = Net::Address::IP::Local->public();
if ($Data =~ '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]') {
print($IntStr . $Data . "\n")
} else {
die('Invalid internal IP address detected')
}
}
if ($External eq 'true') {
use HTTP::Tiny;
my $Tiny = HTTP::Tiny->new();
my $Response = $Tiny->get('https://whatismyip.akamai.com');
if ($Response->{'success'}) {
my $Data = $Response->{'content'};
if ($Data =~ '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]') {
print($ExtStr . $Response->{'content'} . "\n")
} else {
die('Invalid external IP address detected')
}
} else {
die('Unable to determine external IP address')
}
}