Skip to content

10.18 Linux下网卡重命名

A: lgx@nsfocus 2005-11-22 10:09


/ * gcc -Wall -pipe -O3 -s -o ifname ifname.c /

include

include

include

include

include

include

include

include

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