[转载] Linux下网卡重命名
文章作者:lgx信息来源:scz is blog
/*
* gcc -Wall -pipe -O3 -s -o ifname ifname.c
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <errno.h>
int main ( int argc, char * argv[] )
{
int s;
struct ifreq ifr;
if ( 3 != argc )
{
fprintf( stderr, "Usage: %s old_name new_name\n", argv[0] );
return( -1 );
}
s = socket( PF_INET, SOCK_DGRAM, 0 );
/*
* 这里有溢出,懒得管了
*/
strcpy( ifr.ifr_name, argv[1] );
strcpy( ifr.ifr_newname, argv[2] );
if ( ioctl( s, SIOCSIFNAME, &ifr ) < 0 )
{
perror( "SIOCSIFNAME" );
}
close( s );
return( 0 );
} /* end of main */
--------------------------------------------------------------------------
# ifconfig eth1 down
# ./ifname eth1 lgx
# ifconfig lgx up
页:
[1]