feat: Update get network device name by nmcli.
This commit is contained in:
@@ -31,6 +31,8 @@ namespace
|
||||
}
|
||||
return QString::number(cidr);
|
||||
}
|
||||
|
||||
const QString NMCLI_CONNECTION_NAME = "usct";
|
||||
}
|
||||
|
||||
NetworkManager* NetworkManager::getInstance()
|
||||
@@ -44,12 +46,14 @@ NetworkManager::NetworkManager(QObject* aParent)
|
||||
, mIsDHCP()
|
||||
, mIpAddress()
|
||||
, mSubNetMask()
|
||||
, mDeviceName()
|
||||
{
|
||||
initNetworkInfo();
|
||||
}
|
||||
|
||||
void NetworkManager::initNetworkInfo()
|
||||
{
|
||||
mDeviceName = getDeviceName(NMCLI_CONNECTION_NAME);
|
||||
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
||||
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()
|
||||
{
|
||||
return JsonObject::Instance()->getServer(JsonObject::LOCAL);
|
||||
|
||||
@@ -35,10 +35,14 @@ public:
|
||||
|
||||
private:
|
||||
explicit NetworkManager(QObject* parent = nullptr);
|
||||
QString getDeviceName(const QString& aNmcliConName);
|
||||
|
||||
private:
|
||||
bool mIsDHCP;
|
||||
QString mIpAddress;
|
||||
QString mSubNetMask;
|
||||
QString mGateway;
|
||||
QString mDeviceName;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user