|
Here is my test code :
system( "echo q | sudo -S ps -A "); return;
and partial output to console
sudo] password for nov25-1: PID TTY TIME CMD
1 ? 00:00:07 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 rcu_gp
4 ? 00:00:00 rcu_par_gp
5 ? 00:00:00 slub_flushwq
6 ? 00:00:00 netns
8 ? 00:00:00 kworker/0:0H-events_highpri
11 ? 00:00:00 mm_percpu_wq
12 ? 00:00:00 rcu_tasks_kthread
13 ? 00:00:00 rcu_tasks_rude_kthread
no output with "resultFile"
hence
same outcome as with original code
PS
ONLY , so far , the "hcitool info" require password AND current system call passing it works as expected.
|
|
|
|
|
This works for me:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int ret;
ret = system ("echo xxxxxx | sudo -S ps -A >results");
printf ("ret=%d\n", ret);
return 0;
} The output goes to "results" file and ret is 0.
Environment: Ubuntu 22.04LTS freshly installed yesterday.
Compiler: gcc 11.1.0
Mircea
modified 19-Feb-24 21:48pm.
|
|
|
|
|
Just to make sure - how is your results file declared /defined?
I am getting correct return value but no data anywhere - no console or file.
|
|
|
|
|
It doesn’t exist before running the program. It is created by the command. If you want to read it, you should open it after the command has finished.
Mircea
|
|
|
|
|
Kindly allow me to "write review".
The OP question was answered.
by placing the redirection as last option of the system call.
Ir was verified by passing by complier / linker and function
returning success ( 0 ).
However, as coded there is no "passed to " function seen in debugger,
( since it is part of the options ?)
and
there is no practical value of the "passed to " function if it cannot be read.
hence there is an extension to my OP
how to read the contents of the "passed to" function ?
after that question extension is answered there will be another
modification
how to "pipe" ca;; result to the console and redirect it to a file SAME time ?
I sincerely appreciate your contribution to resolve this.
|
|
|
|
|
|
I'm not sure I understand your question. Something might be "lost in translation".
You cannot send results to file and console "at the same time". What you can do is send them to the console right after you sent them to the file. For instance:
ps -A >results && cat results
If what you want is to read the results in your program and process them somehow, you could do something like:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int ret;
char line[256];
ret = system ("echo xxxxxx | sudo -S ps -A >results");
FILE *f = fopen("results", "r");
while (!feof(f)) {
fgets (line, sizeof(line), f);
puts(line);
}
fclose(f);
return 0;
} I didn't compile this code but should be rather OK.
If you explain better what you want to do maybe I can answer more to the point.
Mircea
|
|
|
|
|
Mircea Neacsu wrote: You cannot send results to file and console "at the same time"
At the risk of further confusing the OP, I'd like to remind you of the tee command
$ ls | tee /tmp/foo
PID TTY TIME CMD
3995778 pts/1 00:00:00 bash
3995865 pts/1 00:00:00 ps
3995866 pts/1 00:00:00 tee
$ cat /tmp/foo
PID TTY TIME CMD
3995778 pts/1 00:00:00 bash
3995865 pts/1 00:00:00 ps
3995866 pts/1 00:00:00 tee
But to the OP's issue, I'd like to, once again, point out that system() is not an ideal tool to use if you want to capture command output. A better option would be to use popen() or a QProcess object. The masochists could also write their own process object that allows reading and writing to the child process, but that's an adventure not for the faint of heart.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
k5054 wrote: I'd like to remind you of the tee command
True! I forgot about good old tee Too much time spent in Windowsland
Mircea
|
|
|
|
|
I believe it is time to say that the problem has been solved.
I now have enough info to figure out why
hcitool info 98:D3:31:F8:39:33 takes almost 20 seconds to "complete", however , it goes from immediate " connect " to "disconnect" in few seconds...
(hcitool problem outside CodeProject scope )
In retrospect I would like to point out that I am still not sure how
echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33
works
how do I get "user" in console , but not "password"?
There was two reasons why I shy away from QProcess
I am being chastised every time I ask Qt question, but it is OK to keep showing QProcess to me
especialy when I do not know what is "program" in
echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33
command.
(obviously Qt question )
|
|
|
|
|
Salvatore Terress wrote: obviously Qt question No, nothing to do with Qt. But then that is (the main) part of your problem, you seem to be confused between what is Qt, the language library or the Linux shell.
|
|
|
|
|
|
UPDATE ( just to share )
here is working as expected C++ code
system(" echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33");
and here is the result:
[sudo] password for nov25-1: Can't create connection: Input/output error
what is interesting "echo" hides the passed password
the challenge - how to hide [sudo] password for nov25-1:
modified 17-Feb-24 12:05pm.
|
|
|
|
|
Googling provides answers
C++ "sudo" provide password
|
|
|
|
|
Try using a Here Document :
[k5054@localhost]$ sudo ps
[sudo] password for k5054:
sudo: no password was provided
sudo: a password is required
[k5054@localhost]$ cat example.cpp
int main()
{
std::string userPass{"myPassword"};
std::string command{"sudo -S ps -lf 2>/dev/null <<_EOF\n" + userPass + "\n_EOF"};
FILE *pipe = popen(command.c_str(), "r");
char *line = NULL;
size_t len = 0;
ssize_t rlen;
while( (rlen = getline(&line, &len, pipe)) > 0) {
line[--rlen] = '\0'; // trim trailing newline
std::cout << line << '\n';
}
pclose(pipe);
free(line);
}
[k5054@localhost]$ make example
g++ -Wall -Wextra -ggdb example.cpp -o example
[k5054@localhost]$ ./example
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
1 S root 4030 4029 0 80 0 - 4799 do_pol 11:49 pts/2 00:00:00 sudo -S ps -lf
4 R root 4031 4030 0 80 0 - 3502 - 11:49 pts/2 00:00:00 ps -lf
[k5054@localhost]$ You should be able to modify that for use with QProcess.
Note that if you don't redirect stderr to /dev/null, the SSH password prompt goes to wherever stderr is pointing to.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Thanks,
as I suspected "stdin" and file "pipe" are what I failed to put into THE equation.
Unfortunately all these " well meaning " but superficial, distracting from subject , AKA not answering questions , commentaries about security did not help...
SOLVED
10-4
|
|
|
|
|
Hello folks, just a quick question about the Visual Studio Resource Editor:
Is it possible to use it to edit resources in a compiled executable without the source code? (Specifically the VERSION resource)
I doubt it, but I just wanted to be sure.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yes, in the "File Open..." (Ctrl+O) dialog select file type EXE. Select the Version resource, edit it and save the file.
Mircea
|
|
|
|
|
Thanks! I never noticed that capability.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
If the exe (or dll) is signed I am guessing that would not work?
|
|
|
|
|
You still can edit it but it invalidates the signature - makes sense. Never tried it before, thanks for uncovering another detail.
Mircea
|
|
|
|
|
Voluntarily removed- violates "Posting guidelines "
modified 16-Feb-24 12:53pm.
|
|
|
|
|
|
Message Closed
modified 16-Feb-24 12:53pm.
|
|
|
|
|
process->start("sh", QStringList() << "-c" << "xterm");
It looks like you are starting a terminal session via the shell. At a guess the xterm process is running and the shell process that started it is waiting for it to finish. And your application will be waiting until both xterm and its parent shell terminate. At which time I doubt that there will be any stdout to capture.
|
|
|
|