0

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.

14
  • 1
    local_var=$(ssh user@server "echo \${server_var}") works for me Commented Jun 6, 2013 at 18:32
  • 1
    Don't assign the value. Instead, let the command put it's output to standard output. That will tell you if the ssh successfully echoes the remote value or not. Start further debugging after that. Commented Jun 6, 2013 at 18:39
  • 1
    How is the variable being set on the server? ssh user@server "command" doesn't run .profile, so variables set there won't be seen. Commented Jun 6, 2013 at 18:42
  • 1
    @nkon: Variables are not shared between the two sessions, only the authentication. Commented Jun 6, 2013 at 18:45
  • 1
    Connection sharing shares the connection and sshd process, but not the shell process. Commented Jun 6, 2013 at 18:45

2 Answers 2

2

It is typically impossible (ignoring debugger tricks) to find out the value of a variable in the address space of a running process without that process explicitly sharing it. It can write the value to disk, and you can read that file from another process (either run locally, or via ssh).

Sign up to request clarification or add additional context in comments.

3 Comments

To add to this answer, you can do ssh u@s "echo 'name=value' >/some/safe/place ; source /some/safe/place" and then ssh u@s "source /some/safe/place; echo \$name"
@chepner I see what you mean (I think). So this variable is still set even when I establish a new session, albeit now in an inaccessbile address space, so since it's a new session an echo to this variable will result in null since I'm trying to echo a value from a different shell?
Right. Each ssh session starts a new process on the remote server, even if a single ssh process is managing all the connections.
0

Your second example, local_var=$(ssh user@server "echo \${server_var}"), works for me. Are you sure the variable you're trying to read is set when you call ssh like this? Remember when you pass a command to ssh, it creates a non-interactive shell, which results in different shell initialization (and often fewer variables being set).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.