Format Powershell output to a minimum - c++

I'm calling Powershell from QProcess
QProcess p;
p.start("powershell.exe", QStringList() << "get-wmiobject win32_networkadapter -filter 'netconnectionstatus = 2' | select netconnectionid");
if (p.waitForFinished())
while(p.canReadLine())
{
auto line = QString::fromStdString(p.readLine().toStdString());
qDebug() << line;
}
From that, on QtCreator output, I see
"\r\n"
"\r\n"
"netconnectionid : Ethernet\r\n"
"\r\n"
"netconnectionid : VirtualBox Host-Only Network\r\n"
"\r\n"
"\r\n"
"\r\n"
I would like to reduce Powershell output to
"Ethernet"
"VirtualBox Host-Only Network"
How to format Powershell output to a minimum?

Related

double quotes problems in Qstring

QProcess p;
QString aa = "tasklist /FI 'IMAGENAME x32dbg.exe' /FO LIST | findstr 'PID:'";
aa.replace(0x27,0x22);
qInfo() << aa;
p.start(aa.toStdString().c_str());
p.waitForFinished();
qInfo() << "Output:" << p.readAllStandardOutput() << "Error:" << p.readAllStandardError();
// returned error <ERROR: Invalid argument/option - 'x32dbg.exe\"'.\r\nType \"TASKLIST /?\" for usage.\r\n">
qebug return
{tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\"}
the correct text must be
{tasklist /FI "IMAGENAME eq x32dbg.exe" /FO LIST | findstr "PID:"}
i tried with \"
and add the command line in const char *
all return same result
The problem is you cannot run pipes with QProcess, but only a single process. The workaround would be to pass your command as an argument to cmd.exe:
QProcess p;
p.start("cmd.exe", QStringList() << "/C" << "tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\"");
QDebug's quoting option enabled by default, so use noquote().
bool isRunning(const QString &process) {
QProcess tasklist;
tasklist.start(
"tasklist",
QStringList() << "/NH"
<< "/FO" << "CSV"
<< "/FI" << QString("IMAGENAME eq %1").arg(process));
tasklist.waitForFinished();
QString output = tasklist.readAllStandardOutput();
qInfo() << output ;

QProcess on MacOS trying to use diskutil

Qt 5.12
I am trying to get the volume ID on macOS and using the following function:
QString getVolumeInfo()
{
QString volumeID = "Cannot find the volumeID";
QProcess p;
//diskutil info $(df -h / | tail -1 | cut -d' ' -f 1)
QString command = "diskutil";
QStringList args;
args << "info" << "$(df -h / | tail -1 | cut -d' ' -f 1)";
p.start(command, args);
p.waitForStarted();
p.waitForFinished();
foreach(QString line, QString(p.readAll()).split("\n"))
{
if(line.contains("Volume UUID:"))
volumeID = line;
}
return volumeID;
}
I have to use the diskutil because of the limitation with macOS. However, QProcess, reading the object has nothing in it.
Command on terminal: diskutil info $(df -h / | tail -1 | cut -d' ' -f 1)
which returns a ton of information like:
...
SMART Status: Verified
Volume UUID: 954BACF1-EBC5-4D14-86FB-0912CF7F839C
Disk / Partition UUID: 954BACF1-EBC5-4D14-86FB-0912CF7F839C
Disk Size: 500.1 GB (500068036608 Bytes) (exactly 976695384 512-Byte-Units)
....
When I try to add qDebug() to debug I get the following: true - "Could not find disk: $(df -h / | tail -1 | cut -d' ' -f 1)\n"
So seem like the arguments is not formatted or something?
I am trying to implement: https://apple.stackexchange.com/questions/50302/how-can-i-tell-which-volume-the-operating-system-is-on
The slight modification after Eelke explained.
QStringList args;
args << "-c" << "diskutil info $(df -h / | tail -1 | cut -d' ' -f 1)";
p.start("/bin/bash", args);

Qt - How to execute this command?

I'm trying to accept automatically the host key when connecting to a server using this command
QString cmd = QString("echo y | plink.exe -ssh %1 -i root.ppk -l root exit").arg(strSensorAddress)
When I launch it from cmd.exe in Windows it is working.
I have tried to do it in Qt like this but it doesn't work :
QString cmd = QString("plink.exe -ssh %1 -i root.ppk -l root exit").arg(strSensorAddress);
process1.setStandardOutputProcess(&process2);
process1.start("echo y");
process2.start(cmd);
process2.setProcessChannelMode(QProcess::ForwardedChannels);
And like this :
QStringList arguments;
arguments << "plink.exe" << "-ssh" << strSensorAddress << "-i" << "root.ppk" << "-l" << "root" << "exit";
process1.setStandardOutputProcess(&process2);
process1.start("echo y");
process2.start("cmd.exe", arguments);
process2.setProcessChannelMode(QProcess::ForwardedChannels);

Creating tar.gz-archive from c++ program does not work

I use the following code snippet for creating a tar.gz-archive in an extensive measurement software. After collecting some data in several files I want to archive and compress them for later use.
Everything works fine when I start the program from the shell, all the data is collected and archived correctly.
However the program should start automatically after system start of an embedded Linux system. When it's started via a script in /etc/init.d, no data files are archived/compressed, even though I get the return value 0. Furthermore, the tar.gz-file is created, but it's empty.
Everything else is working fine.
Can anyone please explain, what I have to do in this special case of an automatic start?
int returnValue = -1;
std::string jobString = RESULT_PATH;
jobString += "/";
jobString += lastJobString;
std::string jobFiles = lastJobString + "*.*";
std::string cmd = "tar cvf - ";
cmd += jobFiles;
cmd += " | gzip > ";
cmd += jobString;
cmd += ".tar.gz";
std::cout << "archiving and compressing " << jobFiles << ": " << cmd << std::flush << std::endl;
returnValue = system(cmd.c_str());
std::cout << "archiving and compressing finished. Code: " << returnValue << std::flush << std::endl;
I know that there are several librariers, like libarchive, libtar, etc. which to use is not as lazy as firing a system command, but I would like to know why this does not work for my case.
Furthermore, the version of tar in my busy box does not support option z.
I finally found a solution for my problem and maybe for all the cases when a system command is called by a daemon:
The trick is to create a new shell by the command sh and change the current directory before the tar-function is called:
std::string cmd = "sh -c \" cd ";
cmd += SOURCE_DIR;
cmd+= " && tar cvf - ";
cmd += jobFiles;
cmd += " | gzip > ";
cmd += jobString;
cmd += ".tar.gz";
returnValue = system(cmd.c_str());
Maybe this will help other users heading to the same problem.

Unable to start g++ using QProcess

I want to compile a c++ file from Qt application by using QProcess. But it is not working, I don't see any .o or .exe file generated by the compiler.
Here is what I am doing -
QProcess *process = new QProcess(this);
QString program = "g++";
QStringList arguments;
//fileName is fetched from QFileDialog
arguments << fileName << "-o" << QFileInfo(fileName).path() + QFileInfo(fileName).baseName() + ".exe";
errorFilename = QFileInfo(fileName).baseName() + "_error.txt";
process->setStandardOutputFile(errorFilename);
connect(process, SIGNAL(finished(int)), this, SLOT(compiled()));
process->start(program, arguments);
Pleae tell me what's wrong with this code. I am working on windows 7.
Keep in mind that errors don't go to stdout, they go to stderr. Try using:
process->setStandardErrorFile(errorFilename);
Also QFileInfo::path() won't have a path separator at the end, so you'll need to add one when concatenating the path with the base filename:
QFileInfo finfo(fileName);
arguments << fileName << "-o" << QFileInfo( QDir(finfo.path()), finfo.baseName() + ".exe").filePath();