title: How can I retrieve environment variables from remote system in Python?
body: <p>I'm trying to retrieve environment variables of a remote Linux server in Python. Alternatively, if I could use the variable name in the command I'm executing, that'd be great as well. The calls I'm making should work, as far as I can tell, but I'm getting back garbage. I have set up public-key authentication, so no password required.</p>
  
  <p>Effort 1:</p>
  
  <pre><code>devdir = subprocess.check_output(["ssh", connectstring, "echo $DEVDIR"])
  </code></pre>
  
  <p>Effort 2:</p>
  
  <pre><code>ret = subprocess.check_output(["ssh", connectstring, 
      "$DEVDIR/{0}".format(testpath)])
  </code></pre>
  
  <p><code>connectstring</code> is user@ip and works fine. <code>$DEVDIR</code> is the remote variable I want to use and contains a path. <code>testpath</code> is the path to the script I'm trying to execute, rooted at <code>$DEVDIR</code>.</p>
  
  <p>Effort 1 returns "\n", Effort 2 fails to resolve $DEVDIR remotely.</p>
  
  <hr>
  
  <p>Effort 3:</p>
  
  <pre><code>import paramiko
  ...
  ssh = paramiko.SSHClient()
  ssh.connect(ip, user)    # succeeds
  stdin, stdout, stderr = ssh.exec_command("echo $DEVDIR")
  </code></pre>
  
  <p>result: stdout.readlines() = "\n"</p>