Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Can I change the IP by perl it own

by A_Banknote (Initiate)
on Jul 15, 2004 at 11:06 UTC ( [id://374605]=perlquestion: print w/replies, xml ) Need Help??

A_Banknote has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Can I change the IP by perl it own
by gellyfish (Monsignor) on Jul 15, 2004 at 11:43 UTC

    You will need to do the ioctl SIOCSIFADDR on the appropriate device. Perl's ioctl is described in perlfunc beyond that you are on your own.

    /J\

Re: Can I change the IP by perl it own
by tachyon (Chancellor) on Jul 15, 2004 at 14:13 UTC

    Here is how you do it in C. I would just toss this into inline C and call it a day but it would be easy enough to convert to Perl. Note SIOCDIFADDR (to delete old address) will fail and return an einvalue on Linux as it was not implemented in the (older) kernels for IPV4. It works anyway, despite the error message. If you are on BSD or other *nix flavour it should just work.....

    #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <errno.h> int main() { char iface[] = "eth0"; char ip[] = "192.168.1.3"; IP_SetIP(iface,ip); return 0; } int IP_SetIP(const char* interface, const char * ip_address ) { int sock=0; struct sockaddr_in* addr=NULL; struct ifreq ifr; sock = socket( AF_INET, SOCK_DGRAM, 0 ); if( sock == -1 ) { printf("Can't get a socket!"); return (-1); } memset(&ifr,0,sizeof( struct ifreq ) ); strncpy(ifr.ifr_name,interface,IFNAMSIZ); if( ioctl( sock, SIOCGIFADDR, &ifr ) < 0 ) { printf("Can't get IP '%s' because: '%s'\n",interface,strerror( +errno)); } else { if( ioctl( sock, SIOCDIFADDR, &ifr ) < 0 ) { printf("Can't remove '%s' because: '%s'\n",interface,strer +ror(errno)); } } memset( &ifr, 0, sizeof( struct ifreq ) ); addr= (struct sockaddr_in *)&(ifr.ifr_addr); memset(addr, 0, sizeof( struct sockaddr_in) ); addr->sin_family=AF_INET; addr->sin_addr.s_addr=inet_addr(ip_address); strncpy(ifr.ifr_name,interface,IFNAMSIZ); if( ioctl( sock, SIOCSIFADDR, &ifr ) != 0 ) { printf("Can't fix IP of '%s' with '%s' because '%s'\n", interface,ip_address,strerror(errno)); close(sock); return (-1); } else { printf("IP for '%s' set to'%s'\n",interface,inet_ntoa(addr->si +n_addr)); } close(sock); return(0); }

    cheers

    tachyon

      thanks
      I'm sorry for that I igored your post and post a new post
Re: Can I change the IP by perl it own
by matija (Priest) on Jul 15, 2004 at 11:37 UTC
    That depends very much pn the details of your operating system, but since you didn't mention what you're using, how can we help you?

    In any case, that's not a perl question - unless you meant to re-implement ifconfig in perl...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://374605]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found