Is it possible to determine the hardware address of the router?

3

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

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

It returns me this:

"94:39:E5:F2:AB:5D"
"Wi-Fi"
-------------------------

The problem is that I'm not sure if I'm even returning the hardware address of an equipment on the network or my computer's "wi-fi" board, what I really want is to return the same as arp , on my Wi-Fi adapter the gateway is:

arp -a 192.168.0.1

It returns:

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

But I also do not understand if this is really the physical address of the router or interface on the computer.

Is it possible to return an equipment hardware address?

    
asked by anonymous 22.10.2016 / 18:18

1 answer

2

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

  • The arp program is installed on the machine running your program.
  • The output format of arp is always the same (I believe it is).
  • Basically, you run arp , capture the output produced by it, and find the information you want. The code below has been tested on Windows because the output you showed it looks like you're using Windows itself (correct me if I'm wrong). The function below receives 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 catch it without much difficulty.

    QString obterEnderecoFisico(const QString &enderecoIP)
    {
        // A classe QProcess representa um processo.
        QProcess processoARP;
    
        // Esecificar o nome do programa, que é arp.
        processoARP.setProgram("arp");
    
        // Especificar os argumentos.
        // Estou usando os mesmos argumentos que foram mostrados na pergunta.
        processoARP.setArguments(QStringList() << "-a" << enderecoIP);
    
        // Rodar o programa.
        processoARP.start();
    
        // Esperar o processo concluir.
        processoARP.waitForFinished();
    
        // 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:

    ui->label->setText(obterEnderecoFisico("192.168.0.1"));
    

    To change the text displayed in label to the physical address of the machine that has the IP address 192.168.0.1.

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

    Edit:

    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 observe the output of arp when the specified address is not found and adapt the code to deal with it. As I do not know the language you are using (I use Windows in English) I did not implement this part, because I imagine the output would be different.

        
    22.10.2016 / 22:09