通过脚本设置ip地址和网关:
1 std::string setip = "ifconfig eth0 " +lineEdit_NetIp->text().toStdString() + " netmask " + lineEdit_NetMask->text().toStdString() + " && echo 1"; 2 std::string setroute = "echo 1;route del default;route add default gw " + ui->lineEdit_NetGate->text().toStdString(); 3 FILE *fp = popen(setip.c_str(), "r"); 4 if (NULL == fp) 5 { 6 msgBox.setInformativeText(tr("The IP address is unusable!")); 7 msgBox.exec(); 8 return; 9 } 10 char getway[16] = {0}; 11 if (NULL == fgets(getway, 16, fp)) 12 { 13 pclose(fp); 14 msgBox.setInformativeText(tr("The IP address is unusable!")); 15 msgBox.exec(); 16 return; 17 } 18 if (getway[0] != '1') 19 { 20 msgBox.setInformativeText(tr("The IP address is unusable!")); 21 msgBox.exec(); 22 return; 23 } 24 pclose(fp); 25 fp = NULL;
1 fp = popen(setroute.c_str(), "r"); 2 if (NULL == fp) 3 { 4 msgBox.setInformativeText(tr("The route address is unusable!")); 5 msgBox.exec(); 6 exit(0); 7 } 8 9 char route[16] = {0}; 10 11 if (NULL == fgets(route, 16, fp)) 12 { 13 pclose(fp); 14 msgBox.setInformativeText(tr("The route address is unusable!")); 15 msgBox.exec(); 16 exit(0); 17 } 18 pclose(fp); 19 20 if (route[1] != 0) 21 { 22 msgBox.setInformativeText(tr("The route address is unusable!")); 23 msgBox.exec(); 24 exit(0); 25 }