0

I am going to obtain the output of the following Linux command, to get all IP address for connected host in QT.

arp -n | grep 'C'

To achieve this, I wrote the following code:

QProcess proc;
proc.start("arp", QStringList() << "-n" << "|grep 'C'");
proc.waitForFinished();
QString output = proc.readAllStandardOutput();
qDebug()<<output;

int pos = 0;
QStringList ipList;
QRegExp re("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})");
while ((pos = output.indexOf(re, pos)) != -1)
{
    ipList.append(re.cap(1));
    pos += re.matchedLength();
}
qDebug()<<ipList;

But the output is empty when I add grep option! If I remove grep option the output is not empty.

proc.start("arp", QStringList() << "-n");
Abolfazl Diyanat
  • 397
  • 2
  • 13
  • `|` is a shell feature. I think you need to execute `/bin/sh` and give it the arguments to execute shell command line `arp -n | grep 'C'`. `QProcess` doesn't do this automatically, as it does not use shell to launch the subprocess, it launches it directly (I think, didn't check just now). – hyde Jan 12 '20 at 14:43
  • Thanks, As you suggested, I create a new file named as arp.sh and then run it using QProcess – Abolfazl Diyanat Jan 12 '20 at 15:13

0 Answers0