linux C下shell命令调用(设置IP地址)
(2016-10-08 15:38:36)
今天研究了下linux C下的shell命令调用。查资料知道能用system函数或exec族函数来实现,我选用了system。
函数原型:
int system(const char *string);
如设置IP地址,语句为:
system("ifconfig eth0 172.23.2.225");
由于我的程序里,IP地址是由软件盘上输入得到的,存入char型数组netip[16],因此实现设置IP的代码为:
char ipaddress[60];
sprintf(ipaddress, "ifconfig eth0 %s", ipaddress);
system(ipaddress);
同理,可设置网关和子网掩码:
char netset[60];
sprintf(netset, "ifconfig eth0 netmask %s", netmask);
system(netset);
sprintf(netset, "ifconfig eth0 broadcast %s/0", bcast);
system(netset);
其中,netmask和bcast都是从软件盘输入得到的。
其实也可以用一个语句设置IP、子网掩码、网关等:
sprintf(netset, “ifconfig eth0 %s netmask % broadcast %s”, ipaddress, netmask, bcast);
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。