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



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to