Bash: Can't store command output into a variable when executing commands in SSH - bash

My bash script is
#!/bin/bash
ssh <HOST_IP> << EOF
echo "here"
output=$(echo "here")
echo $output
EOF
When I ran it, I got:
here
meaning that the $output variable was empty. How can this be? I SSHed into the host manually and ran the same commands, and was able to get "here" in $output.

Related

bash returns different results when commands are passed to bash shell via ssh or HEREDOC [duplicate]

Below is an example of a ssh script using a heredoc (the actual script is more complex). Is it possible to use both local and remote variables within an SSH heredoc or command?
FILE_NAME is set on the local server to be used on the remote server. REMOTE_PID is set when running on the remote server to be used on local server. FILE_NAME is recognised in script. REMOTE_PID is not set.
If EOF is changed to 'EOF', then REMOTE_PID is set and `FILE_NAME is not. I don't understand why this is?
Is there a way in which both REMOTE_PID and FILE_NAME can be recognised?
Version 2 of bash being used. The default remote login is cshell, local script is to be bash.
FILE_NAME=/example/pdi.dat
ssh user#host bash << EOF
# run script with output...
REMOTE_PID=$(cat $FILE_NAME)
echo $REMOTE_PID
EOF
echo $REMOTE_PID
You need to escape the $ sign if you don't want the variable to be expanded:
$ x=abc
$ bash <<EOF
> x=def
> echo $x # This expands x before sending it to bash. Bash will see only "echo abc"
> echo \$x # This lets bash perform the expansion. Bash will see "echo $x"
> EOF
abc
def
So in your case:
ssh user#host bash << EOF
# run script with output...
REMOTE_PID=$(cat $FILE_NAME)
echo \$REMOTE_PID
EOF
Or alternatively you can just use a herestring with single quotes:
$ x=abc
$ bash <<< '
> x=def
> echo $x # This will not expand, because we are inside single quotes
> '
def
remote_user_name=user
instance_ip=127.0.0.1
external=$(ls /home/)
ssh -T -i ${private_key} -l ${remote_user_name} ${instance_ip} << END
internal=\$(ls /home/)
echo "\${internal}"
echo "${external}"
END

ssh to remote server gives incorrect hostname

Below piece of code is part of my build script & I'm running it from Jenkins as a parameterized build option(node).
It is able to connect to server_b and does the tasks as expected, but the only command not working is the "hostname -f".
It still gives the server_a's hostname value instead of server_b's hostname value.
I'm not sure what exactly I'm doing incorrectly,thanks.
#!/bin/bash
server_b(){
folder="/home/mylogin/server_b"
ssh -tt myuser#server_b.com << EOF
echo "$(hostname -f)" ## tried echo `hostname -f` as well
cd $folder
echo -e "FOLDER: $folder"
<other commands that works fine>
exit
EOF
}
server_b
Try escaping the $ that you want interpreted on the remote machine, eg :
echo \$(hostname -f)

ssh bash receive variable from a remote file

I need to read the variable from a remote file over SSH and compare it. But I get a variable in the wrong format. how to do it correctly?
#!/bin/bash
pass='dpassspass'
user='root#10.10.19.18'
IP="10.2.1.41"
path=/sys/variable/serv
#not work## No such file or directory# write=$(sshpass -p $ovhpass ssh -t $user echo "$IP" > $path)
sshpass -p $pass ssh -t $user << EOF
echo "$IP" > $path
EOF
my_var=$(sshpass -p $pass ssh -t $user "cd /sys_ovh; ./serv.bash")
echo mystart-"$my_var"-myend
read=$(sshpass -p $pass ssh -t $user cat $path)
echo start-"$read"-end
echo start-"$IP"-end
if [ "$read" == "$IP" ]; then
echo "run"
fi
output:
Connection to 10.10.19.18 closed.
-myendt-10.2.1.41
Connection to 10.10.19.18 closed.
-endt-10.2.1.41
start-10.2.1.41-end
Where I make a mistake? How to take data from the SSH?
The vars my_var and read are filled with a string ending with '\r', telling echo to go back to the first column. I think this is a problem with your local script. You can correct that with
tr -d "\r" < myfile > myfile2
Your fundamental problem comes from using unquoted here documents for the commands. You should properly understand in which order the shell interprets these contructs.
ssh remote cmd >file
executes cmd remotely, but first redirects the output from the ssh command to the local file.
ssh remote "cmd >’$file'"
The quotes cause the redirection to be part of the remote command line. The variable file is interpreted first, by the local shell, though.
ssh remote 'cmd >"$file"`
The single quotes prevent the local shell from modifying the command before sending it. Thus, he variable interpolation and the redirection are both handled by the remote shell, in this order.
So your commented-out "not work" command could easily be fixed with proper quoting. However, it will be much more elegant and efficient to use a single remote session, and execute all the commands in one go. Mixing the local variable IP with remote variables calls for some rather elaborate escaping, though. A major simplification would be to pass the value on standard input, so that the entire remote script can be single quoted.
#!/bin/bash
pass='dpassspass'
user='root#10.10.19.18'
IP="10.2.1.41"
result=$(echo "$IP" |
sshpass -p "$pass" ssh -t "$user" '
path=/sys/variable/serv
cat > "$path"
cd /sys_ovh
./serv.bash
cat "$path"')
echo mystart-"${result%$'\n'*}"-myend
echo start-"${result#*$'\n'}"-end
echo start-"$IP"-end
if [ "${result#*$'\n'}" == "$IP" ]; then
echo "run"
fi
The output from the remote shell is two lines; we pick it apart by using the shell's prefix and suffix substitution operators.

Execute a statement after logging in to SSH

I am writing a script in which i have to execute a set of statments after logging in via SSH. So i am using this command
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND "&& exec bash -l"
Now this works if i give COMMAND as any single line statement such as ls or mkdir, but its not working if i put the following in COMMAND
COMMAND= "if [ $B==$A ]
then
echo 'ERROR'
fi "
I tried putting \ at the end of each line,changed the indentation,put it into a single line but its throwing error
fi : command not found
syntax error near unexpected token `&&'
The first problem is the ssh statement itself. ssh command should be run like:
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND && exec bash -l
Second one is that COMMAND variable should be assigned as one line like:
COMMAND="if [ $B==$A ]; then echo 'ERROR'; fi;"
I hope this helps.

Strange bash errors when passing script to ssh

I'm trying to run a local script remotely that I'm calling with some other code. The basic formate is
ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}
I get the error line 24: ${Command}: ambiguous redirect
Command is a string with the name of the script I want to run and its arguments. If I change the script to just print the command as
echo "ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
and then run the command myself it works just fine.
I've tried putting the command in a temp variable and then call it that way, like:
TEMP="ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
$TEMP
echo $TEMP
This results in No such file or directory. Again the echoed version of the command runs just fine at the command line.
Anyone know what I'm doing wrong here?
It seems that executing $TEMP doesn't work correctly, as the whole string 'bash -s' -- < ${Command} is given in argument to ssh. And in fact if you create a file called ${Command} on you remote host you will get an error bash: bash -s: command not found.
A solution is to uses eval like this :
eval $TEMP
This really does what it should.

Resources