feat: Update get network device name by nmcli.

This commit is contained in:
sunwen
2025-07-22 10:53:22 +08:00
parent b8f9571506
commit 753aebd35c
2 changed files with 24 additions and 0 deletions

View File

@@ -31,6 +31,8 @@ namespace
} }
return QString::number(cidr); return QString::number(cidr);
} }
const QString NMCLI_CONNECTION_NAME = "usct";
} }
NetworkManager* NetworkManager::getInstance() NetworkManager* NetworkManager::getInstance()
@@ -44,12 +46,14 @@ NetworkManager::NetworkManager(QObject* aParent)
, mIsDHCP() , mIsDHCP()
, mIpAddress() , mIpAddress()
, mSubNetMask() , mSubNetMask()
, mDeviceName()
{ {
initNetworkInfo(); initNetworkInfo();
} }
void NetworkManager::initNetworkInfo() void NetworkManager::initNetworkInfo()
{ {
mDeviceName = getDeviceName(NMCLI_CONNECTION_NAME);
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &interface : interfaces) for (const QNetworkInterface &interface : interfaces)
{ {
@@ -71,6 +75,22 @@ void NetworkManager::initNetworkInfo()
} }
} }
QString NetworkManager::getDeviceName(const QString& aNmcliConName)
{
QProcess process;
process.start("nmcli", {"-t", "-f", "name,device", "connection", "show"});
process.waitForFinished();
QString output = process.readAllStandardOutput();
foreach (const QString &line, output.split('\n'))
{
if (line.contains(aNmcliConName))
{
return line.split(':').last();
}
}
return QString();
}
host NetworkManager::getLocalHost() host NetworkManager::getLocalHost()
{ {
return JsonObject::Instance()->getServer(JsonObject::LOCAL); return JsonObject::Instance()->getServer(JsonObject::LOCAL);

View File

@@ -35,10 +35,14 @@ public:
private: private:
explicit NetworkManager(QObject* parent = nullptr); explicit NetworkManager(QObject* parent = nullptr);
QString getDeviceName(const QString& aNmcliConName);
private:
bool mIsDHCP; bool mIsDHCP;
QString mIpAddress; QString mIpAddress;
QString mSubNetMask; QString mSubNetMask;
QString mGateway; QString mGateway;
QString mDeviceName;
}; };