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.