Thread: Obtaining Assigned Dynamic IP (Using C/C++, Bash Scripting Or Perl)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    Obtaining Assigned Dynamic IP (Using C/C++, Bash Scripting Or Perl)

    Does anyone know of a way to programmatically obtain my local IP address without
    having to parse it out. The only way I know how is to redirect the output from ifconfig to some sort of program and parse the IP out. Not only does this seem harder than it has to be, it seems to be a bit hackish. (I would prefer to use perl or bash scripting)


    Speaking of hackish:
    In case you're wondering why I need it is because i'm trying to use my computer as a server. I'm too broke to afford a static IP, so I'm going to use my ISP web account as the "static ip" and automatically redirect the page to my current IP address. Everytime I turn my computer on (or my IP changes), I will run a cgi script on my "static ip" web page which will store the new address and be able to redirect visitors to my local computer. I will have this script run automatically so I need to be able to programmtically get my IP.


    (Sorry if this thread is in the wrong place)

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    35
    in case anyone has the same problem, i settled for this (even though this isn't what i wanted):

    ifconfig eth0 | grep "inet addr" | cut -d: -f2 | cut -d" " -f1

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In perl?
    Code:
    $ perl -e 'open fh,"/sbin/ifconfig eth0|";
    while(<fh>){
    if(/inet addr:(\S+)\s/){print "$1\n";last;}
    }; close fh;'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Is this Windows or Unix?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Is this Windows or Unix?
    He's referring to ifconfig which is a utility to configure Linux network interfaces. So, I would assume he's a Linux user.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    35
    Plus it's in the linux programming section

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Does anyone know of a way to programmatically obtain my local IP address without having to parse it out.
    Enter the interface name as in eth0 on the command line.
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <net/if.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    
    #define SIOCGIFCONF 0x8912 /* get interface list */ 
    
    int main(int argc, char *argv[])
    { 
    	int reqcounts = 10, mysocket, index, findit, count, wefoundit=0;
    	struct ifconf ifc; struct ifreq *ifr; struct in_addr *ia; 
    	findit= (argc>1);
    	if(findit && strlen(argv[1]) > 64)
    	{ 
    		fprintf(stderr, "Device name too long, can't use it\n");
    		findit=0; } 
    	mysocket=socket(AF_INET, SOCK_STREAM, 0);
    	ifc.ifc_buf = NULL;
    	ifc.ifc_len = sizeof(struct ifreq) * reqcounts;
    	ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
    	if (ioctl(mysocket, SIOCGIFCONF, &ifc) < 0)
    	{ perror("SIOCGIFCONF"); }
    	ifr = ifc.ifc_req;
    	for (index = 0; index < ifc.ifc_len; index += sizeof(struct ifreq))
    	{ 
    		ia= (struct in_addr *) ((ifr->ifr_ifru.ifru_addr.sa_data)+2);
    		if(findit) count= strcmp(ifr->ifr_ifrn.ifrn_name, argv[1]); 
    		if(!findit)
    			fprintf(stdout, "%6s %-15s\n", ifr->ifr_ifrn.ifrn_name, inet_ntoa(*ia));
    		if (findit && (count==0))
    		{ 
    			fprintf(stdout, "%s\n", inet_ntoa(*ia));
    			wefoundit=1;
    		} 
    		ifr++;
    	} 
    	free(ifc.ifc_buf);
    	fprintf(stderr, "exiting with %i\n", wefoundit);
    return wefoundit;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bash scripting?
    By Draco in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 06:15 AM
  2. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM