QProcess on MacOS trying to use diskutil - c++

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

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?

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

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

Using a shell commands in c++

Here's a terminal command:
awk '/^Mem/ {print $4}' <(free -m)
Here's my code:
class CSystemInfo{
public:
std::string free_ram(){
std::string s;
FILE *in;
char buff[512];
//here is a prepared string (but with an error)
if(!(in = popen("awk '/^Mem/ {print $4 \"\\t\"}' <(free -m)","r"))){
return "";
}
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << s;
s=s+buff;
}
pclose(in);
return s;
}
};
CSystemInfo info;
std::string ram = info.free_ram();
cout << ram;
The above code, when run, returns this message:
sh: 1: Syntax error: "(" unexpected
How can I place the '/' symbol, for this command to work correctly?
Your problem is not in C++. You are invoking your command with popen, and popen runs your command in sh shell, that does not support <() syntax, while in your terminal you are having bash, zsh or any other shell, that does support <() syntax.
Edit: Better choise! Use pipes!
popen("free -m | awk ...")
Original answer, not working!: Try invoking bash in popen:
bash -c "awk '/^Mem/ {print $4}' <(free -m)"
in code:
popen("bash -c \"awk '/^Mem/ {print $4}' <(free -m)\"")

FFMPEG and convert swf to image?

I want to use ffmpeg to convert swf to png ,and I can't extract image from some kind of swf like: http://rapidshare.com/files/450953994/Movie1.swf
and I use this code in bat file(1.bat)
cws2fws Movie1.swf 3.swf
ffmpeg -i 3.swf -f image2 -vcodec png tese%d.png
Please help me!!
I only want to convert swf to image also suggestion other way sound helpful?
Mencoder doesn't support the compression [swf # 0xc230a0]Compressed SWF format not supported. Give a try to http://www.swftools.org/download.html (I have tried myself after compiling swftools but without success). swfextract return
$ swfextract test.swf
Objects in file test.swf:
[-i] 1 Shape: ID(s) 1
[-f] 1 Frame: ID(s) 0
No video, no sound, no png…
Update −−−−−−
After several errands, swfrender from swftools do the job. There is a non documented pagerange option. From swfrender.c:
int args_callback_option(char*name,char*val)
{
if(!strcmp(name, "V")) {
printf("swfrender - part of %s %s\n", PACKAGE, VERSION);
exit(0);
} else if(!strcmp(name, "o")) {
[…]
} else if(!strcmp(name, "p")) {
pagerange = val;
return 1;
} else if(!strcmp(name, "s")) {
[…]
return 0;
}
Now knowing that, you could do a shell script (here quick and dirty with bash):
#!/bin/bash
let count=1
swfinput=$1
while :
do
output=`basename $swfinput .swf`$count.png
swfrender $swfinput -p $count -o $output
if [ ! -f $output ];
then
break
fi
echo swfrender $swfinput -p $count -o $output
((count++))
done
That's it…