您的位置:首页 > 运维架构 > Shell

程序调用shell设置ip地址

2013-04-19 09:24 274 查看
通过脚本设置ip地址和网关:

std::string setip = "ifconfig eth0 " +lineEdit_NetIp->text().toStdString() + " netmask " + lineEdit_NetMask->text().toStdString() + " && echo 1";
std::string setroute = "echo 1;route del default;route add default gw " + ui->lineEdit_NetGate->text().toStdString();
FILE *fp = popen(setip.c_str(), "r");
if (NULL == fp)
{
msgBox.setInformativeText(tr("The IP address is unusable!"));
msgBox.exec();
return;
}
char getway[16] = {0};
if (NULL == fgets(getway, 16, fp))
{
pclose(fp);
msgBox.setInformativeText(tr("The IP address is unusable!"));
msgBox.exec();
return;
}
if (getway[0] != '1')
{
msgBox.setInformativeText(tr("The IP address is unusable!"));
msgBox.exec();
return;
}
pclose(fp);
fp = NULL;


fp = popen(setroute.c_str(), "r");
if (NULL == fp)
{
msgBox.setInformativeText(tr("The route address is unusable!"));
msgBox.exec();
exit(0);
}

char route[16] = {0};

if (NULL == fgets(route, 16, fp))
{
pclose(fp);
msgBox.setInformativeText(tr("The route address is unusable!"));
msgBox.exec();
exit(0);
}
pclose(fp);

if (route[1] != 0)
{
msgBox.setInformativeText(tr("The route address is unusable!"));
msgBox.exec();
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: