Qt - How to execute this command? - c++

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);

Related

Format Powershell output to a minimum

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?

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 ;

How to create a new terminal and run a command in it?

I have a function like this
void smbProcess(){
string smbTargetIP;
cout<<"Target IP: ";
cin>>smbTargetIP;
string commandSmb_S = "crackmapexec smb " + smbTargetIP;
int smbLength = commandSmb_S.length();
char commandSmb_C[smbLength + 1];
strcpy(commandSmb_C, commandSmb_S.c_str());
system("xterm -hold -e commandSmb_C");
}
I want to create a new terminal and run my command (like this "crackmapexec smb 192.168.1.0/24"). But it doesn't work. When I try this, it works
system("xterm -hold -e date");
These are also doesn't work
system("xterm -hold -e 'commandSmb_C'");
system("xterm -hold -e "commandSmb_C"");
If you know another way to do this it will works too
Add "xterm -hold -e" to commandSmb_S
void smbProcess(){
string smbTargetIP;
cout<<"Target IP: ";
cin>>smbTargetIP;
string commandSmb_S = "xterm -hold -e crackmapexec smb " + smbTargetIP;
int smbLength = commandSmb_S.length();
char commandSmb_C[smbLength + 1];
strcpy(commandSmb_C, commandSmb_S.c_str());
system(commandSmb_C);
}

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();