Reputation: 639
When I run a C program in this bash script it returns the error.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && det=$(./ossec-rootcheck)">/home/sthh/res
Error:
./ossec-rootcheck: No such file or directory
I want to ssh to a remote machine and then run a program on it. I know that this file is located in that path because when I edit it as you see, it works.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && ./ossec-rootcheck">/home/sthh/res
and as it echo $1
I can see that it does cd /home/sth/remote
right. But I want the return value of that program to be stored in a variable,for example det
.
Upvotes: 1
Views: 445
Reputation: 75458
To get the return code or exit code of the remote code:
ssh -n -f *.*.*.* "cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
To capture errors as well:
ssh -n -f *.*.*.* "exec 2>&1; cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
Also, you probably need to omit && echo \"$1\"
when you find it to be working already. And you could just use single quotes for that:
ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res
Or
ssh -n -f *.*.*.* 'exec 2>&1; cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res
Upvotes: 1
Reputation: 2072
ssh -n -f *.*.*.* "cd /home/sth/remote; echo "$1"; ./ossec-rootcheck || do_your_work">/home/sthh/res
You don't have to store it in a variable.
|| executes do_your_work if the exit status of ossec-rootcheck != 0
If you want to store the numeric exit status in a variable, or echo it, you can do (with proper escaping):
./ossec-rootcheck; ecode=$?; echo $ecode
Upvotes: 2