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

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

Related

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.

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…