PLEASE help (me) with syntax for placings redirection to file.
-
This is my current resource: https://www.ibm.com/docs/en/zos/2.1.0?topic=line-using-redirection-symbols unfortunately I am not sure WHERE the redirection belongs in my system call parameters. Here is my TEST code
QFile \*resultFile; system(" echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 > resultFile"); system(" echo q | sudo -S hcitool info 98:D3:31:F8:39:33 > resultFile"); //system(" echo q | sudo -S hcitool info 98:D3:31:F8:39:33" > resultFile; return;
and the TEST debug result
[sudo] password for nov25-1: Can't create connection: Input/output error
[sudo] password for nov25-1:neither one provides data output to "resultFile".
Please note that the first "system" call attempts to "connect" and fails ,hence
would not have any data to redirect anyway. Expected with "no
connection " behavior.( Used to test compiler /linker. )I am asking for help placing the ">resultFile" into system call.
I don't know what's the difference between the two commands. I don't know what hcitool does, but I don't think that matters. What the command(s) do, is to pass the string "q" as stdin to
sudo
. If "q" is the user password, that's fine because sudo expects to read the user password. On my machine I tried:echo xxxxxx | sudo -S ps -A > results
and that works fine. I get the output of
ps
in theresults
file ("xxxxxx" is the user "password"). Now how useful or portable such an approach is, is another question. - Do you plan to have user's password embedded in your code? What if you change your password? would you recompile the code or what? - Wouldn't it be simpler to just accept that your code has to be run as root? - Maybe you can set hcitool to be run with sudo without a password. Take a look at Sudoers Manual[^]:Quote:
PASSWD and NOPASSWD By default, sudo requires that a user authenticate before running a command. This behavior can be modified via the NOPASSWD tag. Like a Runas_Spec, the NOPASSWD tag sets a default for the commands that follow it in the Cmnd_Spec_List. Conversely, the PASSWD tag can be used to reverse things. For example: ray rushmore = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm would allow the user ray to run /bin/kill, /bin/ls, and /usr/bin/lprm as root on the machine “rushmore” without authenticating himself. If we only want ray to be able to run /bin/kill without a password the entry would be: ray rushmore = NOPASSWD: /bin/kill, PASSWD: /bin/ls, /usr/bin/lprm
Mircea
-
I don't know what's the difference between the two commands. I don't know what hcitool does, but I don't think that matters. What the command(s) do, is to pass the string "q" as stdin to
sudo
. If "q" is the user password, that's fine because sudo expects to read the user password. On my machine I tried:echo xxxxxx | sudo -S ps -A > results
and that works fine. I get the output of
ps
in theresults
file ("xxxxxx" is the user "password"). Now how useful or portable such an approach is, is another question. - Do you plan to have user's password embedded in your code? What if you change your password? would you recompile the code or what? - Wouldn't it be simpler to just accept that your code has to be run as root? - Maybe you can set hcitool to be run with sudo without a password. Take a look at Sudoers Manual[^]:Quote:
PASSWD and NOPASSWD By default, sudo requires that a user authenticate before running a command. This behavior can be modified via the NOPASSWD tag. Like a Runas_Spec, the NOPASSWD tag sets a default for the commands that follow it in the Cmnd_Spec_List. Conversely, the PASSWD tag can be used to reverse things. For example: ray rushmore = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm would allow the user ray to run /bin/kill, /bin/ls, and /usr/bin/lprm as root on the machine “rushmore” without authenticating himself. If we only want ray to be able to run /bin/kill without a password the entry would be: ray rushmore = NOPASSWD: /bin/kill, PASSWD: /bin/ls, /usr/bin/lprm
Mircea
Here is my test code :
system( "echo q | sudo -S ps -A "); //> resultFile"); 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_kthreadno 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.
-
Here is my test code :
system( "echo q | sudo -S ps -A "); //> resultFile"); 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_kthreadno 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
#includeint 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
-
This works for me:
#include
#includeint 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
-
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
-
This is my current resource: https://www.ibm.com/docs/en/zos/2.1.0?topic=line-using-redirection-symbols unfortunately I am not sure WHERE the redirection belongs in my system call parameters. Here is my TEST code
QFile \*resultFile; system(" echo q | sudo -S hcitool cc --role=c 98:D3:31:F8:29:33 > resultFile"); system(" echo q | sudo -S hcitool info 98:D3:31:F8:39:33 > resultFile"); //system(" echo q | sudo -S hcitool info 98:D3:31:F8:39:33" > resultFile; return;
and the TEST debug result
[sudo] password for nov25-1: Can't create connection: Input/output error
[sudo] password for nov25-1:neither one provides data output to "resultFile".
Please note that the first "system" call attempts to "connect" and fails ,hence
would not have any data to redirect anyway. Expected with "no
connection " behavior.( Used to test compiler /linker. )I am asking for help placing the ">resultFile" into system call.
-
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.
-
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.
-
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
#includeint 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
-
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
#includeint 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 teeBut 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 usepopen()
or aQProcess
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
-
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 teeBut 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 usepopen()
or aQProcess
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
-
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 teeBut 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 usepopen()
or aQProcess
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
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 )
-
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
#includeint 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
-
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 )