Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get ip from network device with Perl or C??

2 views
Skip to first unread message

Mats

unread,
Mar 8, 2003, 1:58:25 PM3/8/03
to
Hi!

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.


Julián Albo

unread,
Mar 8, 2003, 2:12:14 PM3/8/03
to
Mats escribió:

> 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.

Kasper Dupont

unread,
Mar 17, 2003, 4:59:06 AM3/17/03
to

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(_);

0 new messages