On Friday 02 Jul 2010 06:08:31 mud_saisem wrote:
> Hi,
>
> I am trying to write a Perl script that will get the ip address of a
> ethernet card using ioctl.
>
> Could somebody please give me a hand. I am guessing that the section
> where I try to pack my ethernet device is where I am going wrong.
>
> There is a python script that I found on the internet that works and
> looks as follows:
> ---------------------------------------------------------------------------
> -------------------------------- import socket
> import fcntl
> import struct
>
> def get_ip_address(ifname):
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> return socket.inet_ntoa(fcntl.ioctl(
> s.fileno(),
> 0x8915, # SIOCGIFADDR
> struct.pack('256s', ifname[:15])
> )[20:24])
>
>
> print get_ip_address('eth0')
>
>
>
>
> I have tried the following with no luck
> ---------------------------------------------------------------------------
> ------ #!/usr/bin/perl
>
> use Socket;
> require 'sys/ioctl.ph';
>
> $proto = (getprotobyname('udp'))[2];
> print "getprotp: $proto\n";
>
> socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) or die "Socket create
> error: $!\n";
>
> $eth = "eth0";
> $pack = pack("s256", $eth);
> print "pack: $pack\n";
>
> ioctl(fileno(SOCKET), SIOCGIFADDR, $pack) or die "Error with ioctl: $!
> \n";
> print "ioctl: ", ioctl(fileno(SOCKET), SIOCGIFADDR, $pack), "\n";
>
> I am getting the following output:
> getprotp: 17
> pack:
> Error with ioctl: Bad file descriptor
Here's a modified version of your script in which I made some progress:
{{{
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
require 'sys/ioctl.ph';
my $proto = (getprotobyname('udp'))[2];
print "getprotp: $proto\n";
my $socket;
socket($socket, PF_INET, SOCK_DGRAM, $proto) or
die "Socket create error: $!\n";
my $eth = "eth0";
my $pack = pack("a256", $eth);
print "pack: $pack\n";
ioctl($socket, SIOCGIFADDR(), $pack) or die "Error with ioctl: $!
\n";
print "ioctl: ", ioctl($socket, SIOCGIFADDR(), $pack), "\n";
}}}
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What Makes Software Apps High Quality - http://shlom.in/sw-quality
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/