title: Can't seem to get pexpect to print data from command
body: <p>I'm steadily working on how to ssh and parse data on a device by running a command. I have had a few questions along the way of this endeavor and much help with the questions I have asked. I'm now working with pexpect and I'm not seeing much in documentation with what I am doing. Basically I need to ssh in, as I said, and then run a command that will print out data, then get that data to print to my console. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>import pexpect
  import pxssh
  import getpass
  
  child = pexpect.spawn('ssh www.example.com')
  password = getpass.getpass('password: ')
  child.sendline ('foo bar')
  data = (child.read_nonblocking(size=1000, timeout=100))
  print data
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>password:
  foo bar
  </code></pre>
  
  <p>In the <code>foo bar</code> command the first line of the print out is <code>foo bar</code> so I am wondering if this is trying to print this data but only printing the first line. I add the <code>read_nonblocking(size=1000, timeout=100)</code> trying to set the size to be greater and a timeout to let the data print.</p>
  
  <p><strong>UPDATE with PXSSH</strong></p>
  
  <p>I have also tried to use the pxssh samples to do this and get only the list of commands that <code>foo</code> can run. I need to get the print out of <code>foo bar</code> which is the list of configs. <strong>My guess is that you can't have commands with spaces?</strong> Here is the code I have tried:</p>
  
  <pre><code>import pxssh
  import getpass
  try:
      s = pxssh.pxssh()
      s.force_password = True
      hostname = raw_input('hostname: ')
      username = raw_input('username: ')
      password = getpass.getpass('password: ')
      s.login (hostname, username, password)
      s.sendline ('foo bar')  # run a command
      s.prompt()             # match the prompt
      print s.before         # print everything before the prompt.
      s.logout()
  except pxssh.ExceptionPxssh, e:
      print "pxssh failed on login."
      print str(e)
  </code></pre>
  
  <p><strong>Which gives me this back in console:</strong></p>
  
  <pre><code>pxssh failed on login.
  could not set shell prompt
  :
  
  Session idle time out is disabled
  
  SSH&gt; unset PROMPT_COMMAND
     Error - Command [unset PROMPT_COMMAND] not found.
     foo [ bar | bart | ran | up
           | cmd | bee | hvac | monkey
           | selective | list | help ]
     check[v,nv,beep] [ list | help ]
     delete [ all | bee | neewb | stuff
            | up | cmd | fooconfig | root
            | app | list | hvac | monkey
            | selective | &lt;filename&gt; | confirmed | list | help ]
     exit [ help ]
     get [ vcf | nvcf | snmpcf | help ] [&lt;filename&gt;]
     verbose [ help ]
     help [ &lt;command&gt; | help ]
       up arrow - brings up old command lines
       down arrow - brings up newer command lines
       right arrow - moves cursor to the right
       left arrow - moves cursor to the left
       insert - inserts a space at the cursor
       delete - deletes character at the cursor
  SSH&gt; PS1='[PEXPECT]\$ '
     Error - Command [PS1='[PEXPECT]\$ '] not found.
     foo [ bar | bart | ran | up
           | cmd | bee | hvac | monkey
           | selective | list | help ]
     check[v,nv,beep] [ list | help ]
     delete [ all | bee | neewb | stuff
            | up | cmd | fooconfig | root
            | app | list | hvac | monkey
            | selective | &lt;filename&gt; | confirmed | list | help ]
     exit [ help ]
     get [ vcf | nvcf | snmpcf | help ] [&lt;filename&gt;]
     verbose [ help ]
     help [ &lt;command&gt; | help ]
       up arrow - brings up old command lines
       down arrow - brings up newer command lines
       right arrow - moves cursor to the right
       left arrow - moves cursor to the left
       insert - inserts a space at the cursor
       delete - deletes character at the cursor
  </code></pre>
  
  <p>And as I mentioned I'm just trying to get the console to print out the <code>foo bar</code> command configs. This is the code I had working with <code>python-exscript</code> before finding out I needed to work in older <code>Python 2.4</code>.</p>
  
  <p><strong>CODE THAT I HAD WORKING IN EXSCRIPT THAT I NEED PEXPECT TO DO</strong></p>
  
  <pre><code>account = read_login()              
  conn = SSH2()                       
  conn.connect('example.com')     
  conn.login(account)           
  
  conn.execute('foo bar')
  data = conn.response
  conn.send('exit\r')               
  conn.close()
  print data
  </code></pre>
  
  <p>Any help on how to get this code to work is greatly appreciated! Thanks!</p>