One of my best scripts

main
Barrington 2023-12-03 15:40:38 +01:00
parent 0284e598b1
commit 77fde237be
Signed by: svragv
GPG Key ID: B39C0BCEE94A4A89
1 changed files with 57 additions and 0 deletions

57
perl/getipinfo Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/perl
# getipinfo
# This script resolves an IPv4 or IPv6 address and passes it to
# ipinfo.io to get information about that IP. You can pass -4 and -6
# parameters to force IPv4 or IPv6
# Usage: getipinfo HOSTNAME
# Where hostname can be a domain name or an IPv4 or IPv6 address.
use Socket qw(AF_INET AF_INET6 getaddrinfo NI_NUMERICHOST getnameinfo NIx_NOSERV);
use Getopt::Std;
use LWP::UserAgent;
use JSON;
use strict;
my $json = JSON->new;
my $ua = LWP::UserAgent->new;
our($opt_4, $opt_6) = 0;
getopts('46');
if (!$ARGV[0]) {
printf <STDERR>, "USAGE: %s (-4|-6) HOSTNAME\n","getipinfo";
exit;
}
my ($err, @result) = 0;
if ($opt_4) {
($err, @result) = getaddrinfo($ARGV[0], "", {family => AF_INET});
} elsif ($opt_6) {
($err, @result) = getaddrinfo($ARGV[0],"",{family => AF_INET6});
} else {
($err, @result) = getaddrinfo($ARGV[0]);
}
if ($err) {
printf("Error resolving: %s\n",$err);
exit;
}
my ($er2r, $hostname, $servicename) = getnameinfo $result[0]->{addr}, NI_NUMERICHOST,NIx_NOSERV;
my $json_obj = $json->decode($ua->get("https://ipinfo.io/$hostname")->decoded_content);
printf("\033[01;2mLocation: \033[01;0m%s, %s, %s\n\033[01;2mIP: \033[01;0m%s\n\033[01;2mTimezone: \033[01;0m%s\n\033[01;2mOrganization: \033[01;0m%s\n\033[01;2mHostname: \033[01;0m%s\n",
$json_obj->{city},
$json_obj->{region},
$json_obj->{country},
$json_obj->{ip},
$json_obj->{timezone},
$json_obj->{org},
$json_obj->{hostname});