[SOLVED] perl: turn ip address to hostname : it works, kind of.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824
Rep:
perl: turn ip address to hostname : it works, kind of.
hello all.
i am writing script that parses suricata alerts.
i want to turn ip addresses to hostnames.
here is the code :
Code:
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
use vars qw( @alerts @ipnumbers $gethostname $hostname $ip_to_host $ip );
@alerts = ( '8.8.8.8:666',
'8.8.8.8:80',
'8.8.4.4:333',
'11.11.11.11:222',
'22.22.22.22:999',
'0.0.0.0:443',
'1.1.1.1:443',
'2.2.2.2:7777'
);
foreach $ip (@alerts) {
#print "$ip\n";
if ($ip =~ /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\:[0-9]{1,7}/) {
#print "$1\n";
push(@ipnumbers, ($1));
}
}
foreach $ip (@ipnumbers) {
gethostname($ip);
}
sub gethostname {
$ip_to_host = $_[0];
if ($ip_to_host =~ /^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/) {
#print "$ip_to_host\n";
$hostname = gethostbyaddr(inet_aton($ip_to_host), AF_INET)
or die "Can't resolve $ip_to_host $!\n";
print "$hostname\n";
} else {
print "blah\n";
}
}
the problem with it is that if it fails to turn ip to hostname it exits. i suspect that that die needs to be changed to something else so that function can carry on turning ip's to hostnames. but my skill atm aren't enough.
Yes, you're asking it to stop with the "or die". As per line one of the die documentation: "die raises an exception" - exceptions cause scripts to stop (unless the exceptions are specifically handled).
The above doc also mentions a warn which appears to print to stderr and continue, so that's probably what you want, but if not then you can probably manually print to stderr yourself.
Another option might be a standard if statement, i.e: "if ( $hostname = gethostbyaddr(...) ) { success-actions } else { failure-actions }"
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,804
Rep:
Quote:
Originally Posted by //////
hello all.
i am writing script that parses suricata alerts.
i want to turn ip addresses to hostnames.
here is the code :
Code:
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
use vars qw( @alerts @ipnumbers $gethostname $hostname $ip_to_host $ip );
@alerts = ( '8.8.8.8:666',
'8.8.8.8:80',
'8.8.4.4:333',
'11.11.11.11:222',
'22.22.22.22:999',
'0.0.0.0:443',
'1.1.1.1:443',
'2.2.2.2:7777'
);
foreach $ip (@alerts) {
#print "$ip\n";
if ($ip =~ /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\:[0-9]{1,7}/) {
#print "$1\n";
push(@ipnumbers, ($1));
}
}
foreach $ip (@ipnumbers) {
gethostname($ip);
}
sub gethostname {
$ip_to_host = $_[0];
if ($ip_to_host =~ /^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/) {
#print "$ip_to_host\n";
$hostname = gethostbyaddr(inet_aton($ip_to_host), AF_INET)
or die "Can't resolve $ip_to_host $!\n";
print "$hostname\n";
} else {
print "blah\n";
}
}
Sounds like you may have something that's working now but I wanted to ask:
Why use that regex to validate the IP address string twice? @ipnumbers should have an IP address already; unless I'm missing something, there's no need to re-validate in gethostname().
Couldn't you have created the IP address list by simply splitting the elements of @alerts on the ":", validating the first part, before pushing it onto @ipnumbers? Unless getgostbyaddr() demands it (and I can't recall ever having used it), you shouldn't need the port number. (At least that's an avenue that leapt out at me. But as Larry Wall supposedly once said "There's more than one way to do it." )
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,804
Rep:
Quote:
Originally Posted by //////
... it doesn't work when using "strict".
it does what i want it to do though, next thing to do is fix it so i can use "strict".
I thought that "strict" might be complaining about something in one of the modules you're using. But... I tried your code -- well, a subset of it, and that's not the problem. You appear to have some undeclared variables -- "$alert", "$$alert" (???), "$l", and maybe others -- that "strict" is probably complaining about. Either include them in the "use vars" statement or declare them with statements like "my ( $alert );".
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824
Original Poster
Rep:
Quote:
Originally Posted by rnturn
I thought that "strict" might be complaining about something in one of the modules you're using. But... I tried your code -- well, a subset of it, and that's not the problem. You appear to have some undeclared variables -- "$alert", "$$alert" (???), "$l", and maybe others -- that "strict" is probably complaining about. Either include them in the "use vars" statement or declare them with statements like "my ( $alert );".
Cheers...
thanks to you.
i added %alert to vars and deleted $$alert from $$alert. it works now with 'use strict;'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.