Commit 225f0db7 by “liusq”

修改ping命令-03

parent 75c1ac2a
......@@ -454,26 +454,21 @@ inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask,
subnetMask = entry.netmask().toString();
// 获取网关地址
QProcess process;
process.start("sudo", QStringList() << "/sbin/ip" << "route");
if (!process.waitForStarted()) {
qInfo() << "Failed to start 'ip route' process.";
continue;
}
if (!process.waitForFinished()) {
process.kill();
qInfo() << "Process 'ip route' timed out.";
QFile file("/proc/net/route");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qInfo() << "Failed to open /proc/net/route file.";
continue;
}
QString output = process.readAllStandardOutput();
QStringList lines = output.split('\n');
foreach (QString line, lines) {
if (line.startsWith("default via")) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(QRegExp("\\s+"));
if (parts.size() >= 3) {
if (parts.size() >= 3 && parts[1] == "00000000") {
gateway = parts[2];
gateway = gateway.mid(6, 2) + ":" + gateway.mid(4, 2) + ":" + gateway.mid(2, 2) + ":" + gateway.mid(0, 2);
gateway = gateway.replace(":", ".");
gateway = QHostAddress(gateway).toString();
return true;
}
}
......@@ -481,7 +476,6 @@ inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask,
}
}
}
}
return false; // Return false if no suitable interface is found or gateway not found
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment