Bash script catch exit code of another bash script over ssh - bash

I'm writing a script execute.sh that ssh to other host and execute bash script from another file. The problem is I don't know how to catch exit code of the file I executed.
My execute.sh is like this:
ssh $REMOTE_USER#$REMOTE_HOST 'bash -s' < ./onvps.sh
and I want execute.sh catch exit code of onvps.sh.
Thank you all.

As the ssh client will exit with the exit code of the remote command, you can check the value of $?. if it return 0, it indicates that command is executed successfully.
in your case
ssh $REMOTE_USER#$REMOTE_HOST 'bash -s' < ./onvps.sh
return_code=$?
# then evaluate
if [ $return_code -eq 0 ]
then
echo "OK"
else
echo "ERROR"
fi
One example
$ more mytest.sh
exit 22
$ ssh myuser#myhost 'bash -s' < ./mytest.sh
$ echo $?
22
whatever you are including in the script will be executed in the remote, so $? will give you the exit code of it.

Related

SSH not exiting properly inside if statement in bash heredoc

So i am running this script to check if a java server is up remotely by sshing into remote. If it is down, i am trying to exit and run another script locally. However, after the exit command, it is still in the remote directory.
ssh -i ec2-user#$DNS << EOF
if ! lsof -i | grep -q java ; then
echo "java server stopped running"
# want to exit ssh
exit
# after here when i check it is still in ssh
# I want to run another script locally in the same directory as the current script
./other_script.sh
else
echo "java server up"
fi;
EOF
The exit is exiting the ssh session and so never gets to the execution of the other_script.sh line in the HEREDOC. It would be better to place this outside of the script and actioned from the exit status of the HEREDOC/ssh and so:
ssh -i ec2-user#$DNS << EOF
if ! lsof -i | grep -q java ; then
echo "java server stopped running"
exit 7 # Set the exit status to a number that isn't standard in case ssh fails
else
echo "java server up"
fi;
EOF
if [[ $? -eq 7 ]]
then
./other_script.sh
fi

ssh to server, run commands and save output of commands on local machine using shell

#!/bin/sh
ssh [username]#[ip] "bash -s" <<EOF
if [condition]
then
echo "success"
else
echo "failure"
fi
EOF
After running these commands, I want to save the result (ie success/failure) in a file on local machine. How do I go about it?
Good to try is the IO redirection:
ssh [username]#[ip] "bash -s" > file.txt <<EOF
[...]
Exit status of ssh is the exit status of the remote command so this should work:
( ssh [username]#[ip] [command] && echo success || echo failure ) > result.txt

How to get a custom return value from an SSH command?

I'm trying to get feedback from SSH but I'm unsure how to go about it. My SSH command is structured as follows:
ssh exampleuser#exampledomain.com 'sudo bash -s' < examplebashscript.sh
I have rsa keys set up and the script runs exactly as expected on the remove server. The end of my bash script is as follows
if [ $ERROR_COUNT -ne 0 ]; then
...
exit 10
else
...
exit 20
fi
However, if I echo $? I get 0 as my response. I presume this 0 is being returned because the SSH command ran successfully? How do I return a a value from the SSH command? I want to return a 10 if there were errors when the script ran and 20 if there were no errrors

Catch SSH exceptions in a UNIX script

I have a background process in my server which updates my ~/.ssh/authorized_keys frequently. If I ssh from my client machine at the very moment it will fail
$ ssh my_server date
SSH Version: OpenSSH_5.3p1
user#my_server's password:
and the ssh will mark the script as failed after a number of tries.
I want to break away and add an exception handling piece to sleep 30 seconds whenever this ssh failure occurs.
Something like
*ssh -o 'StrictHostKeyChecking no' appsrvr01.myserv.com "date" 2> /tmp/error
if [ $? -ne 0 ]
then
echo -e "\n Please wait..\n\n"
sleep 1s
else
echo -e "\n The Environment is ready to use!\n\n"
exit 0
fi*
Is there any better approach as the above snippet will still prompt for password
Maybe you could approach this in one shell script by "flock" on a lock file, and then "flock" in the shell script you run above:
In the script that updates your authorized keys:
(flock 200
# commands here that modify your authorized_keys file
) 200>/tmp/authkey_lock
And around the script piece you have posted above:
(flock 200
ssh -o 'StrictHostKeyChecking no' appsrvr01.myserv.com "date" 2> /tmp/error
if [ $? -ne 0 ]
then
echo -e "\n Please wait..\n\n"
sleep 1s
else
echo -e "\n The Environment is ready to use!\n\n"
exit 0
fi
) 200>/tmp/authkey_lock
Please see "man flock" for for information about flock.

Why exit status always coming 0. What will be the solution

Below code is the part of my shell script.
But I am not able to understand why exit status(sshStatus) always coming 0?
I want ssh output as well as exit status.
Please help me to find the solution.
local output="$(ssh -q -o ConnectTimeout=10 \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
$user#$host "$command" 2>&1)"
local sshStatus=$?
command can be :
command="[ ! -d /home/upendra/dfs ]"
command="cat /home/upendra/a.txt"
command="sh /home/upendra/dfs/bin/start-datanode.sh"
Whenever i'm calling command like below directly on shell prompt:
ssh -q -o ConnectTimeout=10 \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
upendra#172.20.20.2 "[ ! -d /home/upendra/dfs ]" 2>&1
Then exit status(echo $?) is coming 1. This is correct because this directory not exists on host.
I got solution on this page :bash shell - ssh remote script capture output and exit code? it is due to "local output". – Upendra
You are always getting exot status as 0 as your command is excecuted successfully
The exit status you obtain from the local machine is the exit status of the last command in ssh session
for example
$ ssh localhost
$ exit 5
$ echo $? #on local system
5
Consider a case without any command
$ ssh localhost
$ ls
#will list commands and exit succussfully
ctrl+d
$ echo $? #on local system
0
Here the exit status of ls command is 0 which is printed.
Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful command returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).
#!/bin/bash
echo hello
echo $? # Exit status 0 returned because command executed successfully.
lskdf # Unrecognized command. echo $? # Non-zero exit status
returned -- command failed to execute.
echo
exit 113 # Will return 113 to shell.
# To verify this, type "echo $?" after script terminates.
Your code return exit code 0 which means your shell script execute successfully.

Resources