Reputation: 205
I am trying to get the value of uname -r from a remote machine over ssh and use that value in my local script flow.
kern_ver=uname -r
sshpass -p "$passwd" ssh -o StrictHostKeyChecking=no root@$c 'kern_ver=echo \$kern_ver'
But looks like the value is not getting passed back to the local script flow .
Upvotes: 1
Views: 4650
Reputation: 295954
Capture is var=$(...)
, as always.
ssh is a bit interesting because it unconditionally invokes a remote shell, so working with completely arbitrary commands (as opposed to simple things like uname -r
) requires a different technique:
filename="/path/to/name with spaces/and/ * wildcard characters *"
printf -v cmd_str '%q ' ls -l "$filename"
output=$(ssh "$host" "$cmd_str")
This way you can use arguments with spaces, and they'll be passed with correct quoting through to the remote system (with the caveat that non-printable characters may be quoted with bash-only syntax, so this is only guaranteed to work in cases where the remote shell is also bash).
Upvotes: 1
Reputation: 532418
Storing the command in the variable cmd
is optional; you can hard-code the command as a string argument to ssh
. The key is that you simply run the command on the remote host via ssh
, and capture its output on the local host.
cmd="uname -r"
kern_ver=$(sshpass -p "$passwd" ssh -o StrictHostKeyChecking=no root@"$c" "$cmd")
Upvotes: 7