nslookup script to loop through IP addresses in perl - perl

Stackoverflow community,
I am bran new to PERL scripting and need help [part of this script was from an internet source] to do an nslookup on a list of IP addresses in a file and loop through each one until I get to the end. If the domain name does not exist then do X, if it does do Y.
information for the below :
-existent - is a key word when nslookup aborts
name - is a key word when it works
listofhosts - is my ip address list
Thank you any help is greatly appreciated
#!/usr/bin/perl
#!c:\perl64\bin
use strict;
use warnings;
my $noname=-existent;
my $name=name;
open IPADDRESSES,("c:\\perl64\\scripts\\listofhosts.txt") or die("File could not be opened :$!");
my #list=<IPADDRESSES>;
foreach my $list(#list);
my $results=`nslookup $list`;
CHOMP ($list);
if ($noname) {
print ("no name")}
elsif ($name){
print ("IP address $list:\n");
print ("=\n");
print ("DNS name:$results\n");
}
close (IPADDRESSES);

Also try looking at Net::Nslookup instead of using nslookup ...

Related

perl variable perl variable

my code :
if ($funcarg =~ /^test (.*)/) {
my $target1 = "http://$1/script/so.php";
system("wget $target1");
so, when i type perl a.pl 127.0.0.1 the script must download http://127.0.0.1/script/so.php, but unfortunately it doesn't. where is my mistake?
[root#localhost perl]# perl a.pl 127.0.0.1
http:///script/so.php: Invalid host name.
It's not clear how your command line argument gets from #ARGV to $funcarg. Or how it goes from 127.0.0.1 to (presumably) test 127.0.0.1. And I think that's where you're going wrong. I think that $funcarg doesn't contain what you think it does before you run the regex match.
This code does what I think you want. But I've had to make up two lines (as marked with a comment) and I'm pretty sure that your version of those two lines is where your misunderstanding is.
#!/usr/bin/perl
use strict;
use warnings;
#ARGV or die "No argument given\n";
# I've made up the next two lines. But I think
# this is where your bug is.
my $host = $ARGV[0];
my $funcarg = "test $host";
if ($funcarg =~ /^test (.*)/) {
my $target1 = "http://$1/script/so.php";
print "$target1\n";
system("wget $target1");
}

Passing Parameteres to Interactive script programatically

We have an interactive script(Script 1) which asks for an IP Address and continues it's execution process. Script 1 is called from script2.
As we know the IP address we want to pass IP Automatically to script so that manual intervention is not required
I looked into Expect Module. But i cannot install that module in PRODUCTION server.
Can someone suggest a way to overcome this issue.
Try this,
#script2.pl
use strict;
use warnings;
use Getopt::Long;
GetOptions (
"ipAddress=s" => \$ip,
) or die("Enter IP address");
my $cmd = "perl script1.pl --ip=$ip";
system($cmd);
.
#script1.pl
use strict;
use warnings;
use Getopt::Long;
GetOptions (
"ip=s" => \$ip,
) or die("Enter IP address");
print "IP address is $ip";
Execute like this.
perl script2.pl --ipAddress=10.11.12.13
If you want to execute script1 directly, can execute like this,
perl script1.pl --ip=10.11.12.13

Perl read line string without trailing newline

Very new to perl and have been stuck for quite awhile on this.
If I change the variable from READSTDIN to google.com, it says google.com is online as it should. If I use the STDIN and input google.com and print $host it prints google.com, however in the ping it doesn't work.
Sample output:
perl perl.pl
What is the website that is offline or displaying an error?google.com
Warning: google.com
appears to be down or icmp packets are blocked by their server
Code:
use strict;
use warnings;
use Net::Ping;
#optionally specify a timeout in seconds (Defaults to 5 if not set)
my $timeout = 10;
# Create a new ping object
my $p = Net::Ping->new("icmp");
#Domain variable
print "What is the website that is offline or displaying an error?";
my $host = readline STDIN;
# perform the ping
if ( $p->ping( $host, $timeout ) ) {
print "Host $host is alive\n";
} else {
print "Warning: $host appears to be down or icmp packets are blocked by their server\n";
}
# close our ping handle
$p->close();
If I change the variable from READSTDIN to google.com, it says google.com is online as it should. If I use the STDIN and input google.com and print $host it prints google.com, however in the ping it doesn't work. I appreciate anyone who can help me at all!
Note the newline in your input:
perl perl.pl
What is the website that is offline or displaying an error?google.com
Warning: google.com <--- newline after google.com puts the rest of the output on the next line...
appears to be down or icmp packets are blocked by their server
You should be using chomp to remove the newline from your input:
chomp( my $host = readline STDIN );
Or more simply:
chomp( my $host = <STDIN> ); # same thing as above

Getting MAC address of another host using perl

I'm trying to find the MAC address of a host if I only have its IP address in perl. I have the following code, but its throwing an error.
#!/usr/bin/perl
use Net::ARP;
$mac = Net::ARP::arp_lookup('eth0','192.168.1.9');
print "$mac";
When I run the code I get the following error,
"SIOCGARP: No such device or address".
I know the device is the correct one I want to use, and I know the IP address is connected to the network and is valid. Any ideas?
i was googling for SIOCGARP: No such device or address and i just found this thread here.
by the way i solved this by pinging the target using :
use Net::Ping;
#my $target = "SOME TARGET";
my $p = Net::Ping::new->('icmp');
$p->ping($target, 1);
#...
#...
#...
hope this could help.
V1R4N64R
Hi you can use this linux commands if you want.
arping
arpscan
nmap

How do I output error for DNS resolve in Perl?

I have currently a DNS Reverse lookup script which works however there is a small little issue of the script being able to output the DNS system errors.
The problems goes like this:
User keys in false/wrong internet address name etc. "www.whyisthednsnothappening.com"
The script would then clear the screen using system(clear)
The script would then print "can't resolve DNS. The error is due to: various System error"
The script re directs the user back to the same menu/script to type in the name address again.
So the main problem is now step 3 which the script only shows me "Can't resolve DNS. The error is due to: BLANK " Which BLANK is suppose to show errors like "Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at ./showdns.pl line 28, <> line 1." and the menu of the DNS script is located below of the error print.
The Codes:
#!/usr/bin/perl
use IO::Socket;
use warnings;
use strict;
use Term::ANSIColor;
use Socket;
use Sys::Hostname;
print "\nYou are now in Show DNS IP Address!\n\n";
print "*************\n";
print "|DNS Address|\n";
print "*************\n";
print "\nPlease enter a hostname that you wish to view\n\n";
print "\n\nEnter the hostname of Choice Here: ";
my $userchoice = <>;
chomp ($userchoice);
my $hostname = $userchoice;
my $i_addr = scalar(gethostbyname($hostname || 'localhost'));
if ( ! defined $i_addr ) {
my $err = $!;
my $herr = int herror(const char *s);
system('clear');
print("Can't resolve $hostname: $herr, try again");
exec("/root/Desktop/showdns.pl");
exit();
}
my $name = inet_ntoa($i_addr);
my $coloredText = colored($name, 'bold underline blue');
print "\n\nThe hostname IP address is: $coloredText\n\n";
print "Press enter to go back to the main menu\n\n";
my $userinput2 = <>;
chomp ($userinput2);
system("clear");
system("/root/Desktop/simpleip.pl");
Can someone please give advice on the codes? Thanks!
Ah, I see what you mean. The system("clear") call is clearing the $! variable before you have a chance to print the error from gethostbyname.
my $i_addr = scalar(gethostbyname($hostname || 'localhost'));
if ( ! defined $i_addr ) {
my $err = $!;
system("clear");
print("Can't resolve $hostname: $err, try again");
system("/root/Desktop/showdns.pl");
exit();
}
Though as far as I can tell, the particular error gethostbyname returns isn't very meaningful.
You may want to look into putting a loop in your script instead of having it start over using system(). You certainly don't want to continue on to inet_ntoa if there was a failure. Note that inet_ntoa doesn't have anything to do with a DNS lookup; that's done by gethostbyname. inet_ntoa just changes a 4-byte string into the normal 123.123.123.123
printable form of an ipaddress. sprintf("%vd", $i_addr) does the same thing.
Two additional questions:
If you remove the call to
system('clear') Does the error
from gethostbyname get displayed
then?
Why do you use
system('/root/Desktop/showdns.pl')
To call the same script recursively?
Wouldn't it be better to use exec
instead of system? exec terminates
the current process. while system
forks of an entire new process and
waits for that process to exit. So
if your users enter, for example, 20
invalid hostnames, you'll end up
with 20 processes just waiting for
the one that was most recently
created.
Gr,
ldx
Please check the following to resolve the above "dns" issues in perl script.
As DNS server is not running, perl will not resolve the address. so it returns an empty string and inet_ntoa will throw error for that empty string.
If you are using a linux system please verify the following:
a) Check the internet address in the file /etc/resolv.conf
nameserver 172.19.1.11 (IP address of your internet or survice provider)
b) Add "dns" in the /etc/nsswitch.conf file as follows:
hosts: files dns