shell命令的调用,可用system函数或exe族函数实现。
函数原型:
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);