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


In reply to Re: Can I change the IP by perl it own by tachyon
in thread Can I change the IP by perl it own by A_Banknote

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.