title: x11 forwarding with paramiko
body: <p>I'm trying to run a command with <code>paramiko</code> that should be able to open an X window. The script I'm using would something as follows:</p>
  
  <pre><code>import paramiko                                    
  
  ssh_client = paramiko.SSHClient()
  ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh_client.connect('192.168.122.55', username='user', password='password')
  transport = ssh_client.get_transport()
  session = transport.open_session()
  
  session.request_x11()
  stdin = session.makefile('wb')
  stdout = session.makefile('rb')
  stderr = session.makefile_stderr('rb')
  session.exec_command('env; xterm')
  transport.accept()
  
  print 'Exit status:', session.recv_exit_status()
  print 'stdout:\n{}'.format(stdout.read())
  print 'stderr:\n{}'.format(stderr.read())
  session.close()
  </code></pre>
  
  <p>Unfortunately, when I run the script above, I get this output:</p>
  
  <pre><code>Exit status: 1
  stdout:
  SHELL=/bin/bash
  XDG_SESSION_COOKIE=8025e1ba5e6c47be0d2f3ad6504a25ee-1347286654.617967-1932974971
  SSH_CLIENT=192.168.122.1 58654 22
  USER=user
  MAIL=/var/mail/user
  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
  PWD=/home/user
  LANG=en_US.UTF-8
  SHLVL=1
  HOME=/home/user
  LOGNAME=user
  SSH_CONNECTION=192.168.122.1 58654 192.168.122.55 22
  DISPLAY=localhost:10.0
  _=/usr/bin/env
  
  stderr:  
  xterm: Xt error: Can't open display: localhost:10.0
  </code></pre>
  
  <p>If I run the following command in a terminal:</p>
  
  <pre><code>ssh -X user@192.168.122.55 'env; xterm'
  </code></pre>
  
  <p>then I get the same environment variables (some ports changed, though), so I'd say that my environment is correct. However, I'm still missing something to make <code>paramiko</code> work with x11 forwarding.</p>
  
  <p>A couple of things I tried are:</p>
  
  <ul>
  <li>Use the <code>handler</code> parameter in <code>request_x11</code>: aside from printing values, I didn't get any further than with the default handler.</li>
  <li>Use the <code>auth_cookie</code> parameter in <code>request_x11</code>: tried to hardcode a cookie value that was being used according to the <code>xauth list</code> output. The idea of doing this was to avoid problems that might happen according to the documentation string in <code>paramiko</code> itself:</li>
  </ul>
  
  <blockquote>
    <p>If you omit the auth_cookie, a new secure random 128-bit value will be
    generated, used, and returned.  You will need to use this value to
    verify incoming x11 requests and replace them with the actual local
    x11 cookie (which requires some knoweldge of the x11 protocol).</p>
  </blockquote>
  
  <p>Is there some other thing I could do to make it work or troubleshoot the problem?</p>
  
  <p>Note:
  This has been previously asked in:</p>
  
  <ul>
  <li><a href="https://superuser.com/q/217420/117041">superuser</a>: the only response points to the <code>request_x11</code> documentation I've already tried to use to no avail.</li>
  <li><a href="https://stackoverflow.com/q/4448089/183066">stackoverflow</a>: the accepted response suggests to use the <code>handler</code> parameter, but it's wrong.</li>
  <li><a href="https://github.com/paramiko/paramiko/issues/40" rel="noreferrer">github</a>: no answer provided for more than a year.</li>
  </ul>