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
Related
I'm new to perl.I was trying to login to a cisco router to execute few commands.
I was able to login using Net::SSH::Perl and execute a couple of commands.
But when i tried to switch to the configure mode the script fails.
br-waas#
br-waas#configure
br-waas(config)#
Is there a perl cpan module to do this?
use Net::SSH::Perl;
my $host = "XX.XX.XX.XX";
my $user = "XXXX";
my $password = "XXXX";
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $password);
print "check the version of the build \n";
my ($stdout) = $ssh->cmd("show statistics accelerator http object-cache");
print $stdout;
sleep(3);
print "enter the config mode \n";
my($stdout1) = $ssh->cmd("config");
print "$stdout1 \n";
When the configure command executes the script fails with the following error. % Invalid input detected at '^' marker.
If you're having trouble with Net:SSH, Net::OpenSSH has always done the job well for me.
If you have an enable password, at the end of the day, you're going to need something implementing Expect, which is always a hassle.
The one library that worked for me is NET::Appliance::Session.
It is reliable, solves the "channel locking" issue en cisco routers/switches and can send commands without the use of expect (or at least does it internally can't remember well)
Does the command "config" work when doing this by hand? I assume you will get a question echoed back on the terminal.
If this is a plain Cisco router running IOS, the command to enter config mode is "configure terminal".
This problem is really puzzling to me: I have the following script working on vmware server 2.0:
#!/usr/local/bin/perl
# server (transmitter)
use strict;
use IO::Socket::Multicast6;
use IO::Interface;
use constant GROUP => "235.1.1.2";
use constant PORT => "3000";
my $sock = IO::Socket::Multicast6->new(
Proto=>"udp",
Domain=>AF_INET,
PeerAddr=>GROUP,
PeerPort=>PORT);
$sock->mcast_if("eth1");
$sock->mcast_ttl(10);
while (1) {
my $message = localtime();
$sock->send($message) || die "Could not send: $!";
} continue {
sleep 4;
}
It works great on vmware server. I have cloned this VM to an EXSi server but running the same exact copy of the virtual machine running the script, and I get the following error:
Can't call method "mcast_if" on an undefined value
Im really puzzled by this as I am not sure what the problem could be.
there is really nothing different except for the CPU running on both machines, but I don't see how something so low level can be causing an issue but I could be wrong. perl -d wasn't very helpful.
Thanks.
It's failing to create the socket, use some error checking to try to find out why. Eg:
my $sock = IO::Socket::Multicast6->new(
Proto=>"udp",
Domain=>AF_INET,
PeerAddr=>GROUP,
PeerPort=>PORT)
or die "Socket failed: $!";
The new() constructor is failing, but not raising an exception. I don’t know its API: is there some way to get it to tell you why?
Otherwise you might try errno (that is, $!).
I am using the Net::FTP in perl to do some file transfers. when I run the following code :-
Are there any issues with using the IP address ? Am I correct in providing this in the host field ?
use strict;
use Net::FTP;
my $host = "10.77.69.124";
my $user = "administrator";
my $password = "Password";
my $f = Net::FTP->new($host, Debug =>0) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";
The code is not able to connect to the remote host. Why is this happening ? Shouldn't this work with the IP address provided in the $host ?
The constructor of Net::FTP allows you to pass a single scalar value or an array of hosts to try. The value of this field should be the same as the PeerAddr from IO::Socket::INET (either a hostname or an ip address).
Have a closer look at what is happening by specifying Debug. If you are behind a firewall or a NAT setup, you should probably also set Passive to a non-zero value and make sure to check if the constructor failed by printing out $#.
my $ftp = Net::FTP->new(Host=>$host, Debug=>1, Passive=>1) || die $#;
If the constructor succeeded, you might want to check if any of the other methods fail:
$ftp->login($user, $pass) || die $ftp->message;
$ftp->cwd($path) || die $ftp->message;
By the way: If you are unsure if you've used the correct host parameter, you can ask Net::FTP which host it tried to connect to:
print $ftp->host, "\n";
If this still doesn't work, please provide a detailed output of your application.
Hope this helps.
First be sure that you can reach the remote side:
From command line use telnet (available on linux and windows too, a it different in syntax)
telnet host 21
If you are not able to connect the from commandline, check for firewall rules or maybe your FTP server running on different port?
If you are able to connect try out login with plain FTP commands:
USER user#remote.host
PASS yourpassword
This will use ACTIVE ftp connection to the remote. This is the old way.
Nowadays most ftp server use PASSIVE ftp. To test try this command out (from linux commandline)
ftp -v -p host
In perl you could use passive mode this way:
my $f = Net::FTP->new($host, Debug =>1, Passive => 1) or die "Can't open $host\n";
I hope this will help you.
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
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new('$host',22);
$ssh->login('$user','$pass');
my $out = $ssh->cmd("show clock");
print $out;
I have the above script to have an ssh session using perl but I'm having the error message
"Can't map service name 'ssh' to port number". I'm using Windows OS. Please advise me where I'm wrong.
Try adding ssh to your services file. The services file is located at:
%SystemRoot%\system32\drivers\etc\services
The line that you'll want to add will look like:
ssh 22/tcp # Secure Shell Login