c++ – Is it possible to determine the hardware address of the router?


I'm trying to get the hardware address of another equipment (actually directly connected equipment, such as a modem/router) on the same network, I managed to do this:

foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
    if (netInterface.flags().testFlag(QNetworkInterface::IsUp) &&
        qDebug() << netInterface.hardwareAddress();
        qDebug() << netInterface.humanReadableName();
        qDebug() << "-------------------------";

He returns this to me:


The problem is that I'm not sure if I'm actually returning the hardware address of a device on the network or my computer's Wi-Fi "card", what I'd really like is to return the same as arp , on my WiFi adapter -Fi the gateway is:

arp -a

He returns:

Endereço IP Endereço físico Tipo 30-b5-c2-21-a6-66 dinâmico

But I also don't understand if this is really the physical address of the router or the interface on the computer.

Is it possible to return a device's hardware address?


The solution I'm going to show you here only works if:

  1. The arp program is installed on the machine running your program.
  2. The arp output format is always the same (I believe it is).

Basically, you run arp , capture the output produced by it, and find the desired information. The code below has been tested on Windows because from the output you showed it looks like you're using Windows anyway (fix me if I'm wrong). The function below takes the IP address as a string and returns the physical address of the machine that has that IP address. The code is well commented, I hope you can get it without much difficulty.

QString obterEnderecoFisico(const QString &enderecoIP)
    // A classe QProcess representa um processo.
    QProcess processoARP;

    // Esecificar o nome do programa, que é arp.

    // Especificar os argumentos.
    // Estou usando os mesmos argumentos que foram mostrados na pergunta.
    processoARP.setArguments(QStringList() << "-a" << enderecoIP);

    // Rodar o programa.

    // Esperar o processo concluir.

    // Ler toda a saída produzida pelo processo.
    QString saida = processoARP.readAll();

    // Separar a saída por linhas.
    QStringList saidaEmLinhas = saida.split('\n');

    // Obter a linha que contém a informação desejada.
    QString linhaImportante = saidaEmLinhas.at(3);

    // Cada campo da linha importante é separado por vários espaços.
    // Precisamos obter cada campo dessa linha.
    QStringList linhaImportanteEmPartes = linhaImportante.split(' ', QString::SkipEmptyParts);

    // O endereço físico é o segundo campo.
    QString enderecoFisico = linhaImportanteEmPartes.at(1);

    // Retornar o endereço físico como uma string.
    return enderecoFisico;
} // É isso.

Then you use it like this:


To change the text displayed in label to the physical address of the machine which has the IP address

The code you posted in the question determines the physical address of the network interfaces of the machine running your program (not the router).


I forgot to mention that for this to work, you must specify the IP address of a machine that is on the local network, otherwise arp will not find a machine with that IP address and the output will not be in the expected format. If you want to make this function more robust, you can watch the arp output when the specified address is not found and adapt the code to handle it. As I don't know the language you are using (I use Windows in English) I didn't implement this part, as I imagine the output would be different.

Scroll to Top