My other question here Assign variable from SSH server to local machine variable was never successfully answered, and I'm not sure how to "bump" the question to get more views, but how can I set the value of my local variable to the value of a variable on an ssh server?
#!/bin/sh
ssh_cmd="ssh-t -o StrictHostKeyChecking=no -o ControlPath=~/.ssh/master-$$ -o ControlMaster=auto -o ControlPersist=10"
local_var=""
$ssh_cmd user@server " server_var=\"test\" "
local_var=$($ssh_cmd user@server 'echo $server_var')
echo $local_var
This will result in a null.
EDIT
I'm using ssh connection sharing and I thought that the entire session (along with the connection was shared), thus I was establishing an initial connection and setting the variable, and reconnecting to that session to echo out the variable that was set.
I have a program on an ssh server that retrieves the IP address of a virtual machine. My local script needs this address so that I run something against the virtual machine from my local script.
The ssh server does much more than just get the IP address, but that one functionality is something I need to know on my local script.
local_var=$(ssh user@server "echo \${server_var}")works for messh user@server "command"doesn't run .profile, so variables set there won't be seen.