I got two network card's in my Linux box, of wich one is assigned IP with
DHCP. I wan't (preferable with perl) get the IP adress by giving the name of
the network device like: IP = Get_IP_from_NIC("eth0")
How would i go about doing that simplest in perl? I don't want to parse the
output from ifconfig or something, but a "clean" solution.
> I got two network card's in my Linux box, of wich one is assigned IP with
> DHCP. I wan't (preferable with perl) get the IP adress by giving the name of
> the network device like: IP = Get_IP_from_NIC("eth0")
This code was recently posted in a spanish language group:
-------8<-------- Corta aquí ----------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void)
{
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
int sockfd, i;
bzero(&ifr, sizeof(ifr));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket()");
return(-1);
}
// Pon el nombre de la interfaz cuya dirección
// quieras en la siguiente línea en el lugar de 'eth0'
strcpy(ifr.ifr_name, "eth0");
sin->sin_family = AF_INET;
if(ioctl(sockfd, SIOCGIFADDR, &ifr) == 0)
{
printf("\n%s : [%s]\n", ifr.ifr_name, inet_ntoa(sin->sin_addr));
}
}
---------8<--------- Corta aquí -------------
Regards.
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#IP
--
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:aaa...@daimi.au.dk
for(_=52;_;(_%5)||(_/=5),(_%5)&&(_-=2))putchar(_);