nullByteMe
nullByteMe

Reputation: 6391

How to set local variable to value of variable on an ssh server

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.

Upvotes: 0

Views: 1789

Answers (2)

chepner
chepner

Reputation: 530960

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).

Upvotes: 2

Adam H. Peterson
Adam H. Peterson

Reputation: 4591

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).

Upvotes: 0

Related Questions