title: How to get return value for expect in python
body: <p>Hi I'm new to python programming.</p>
  
  <p>I have written python script to connect remote host and execute few commands.
  I have used pexpect for this</p>
  
  <pre><code>host = sys.argv[1]
  user = sys.argv[2]
  password = sys.argv[3]
  
  child = pexpect.spawn("ssh -l %s %s "%(user, host))
  child.expect('password:')
  child.sendline(password)
  child.expect('[#\$] ')
  child.sendline('command1')
  child.expect('[#\$] ')
  print child.before
  child.sendline('command2')
  child.expect('[#\$] ')
  print child.before
  </code></pre>
  
  <p>Here I want to execute command 2 if command 1 is successful else I want to send output as NULL and exit.</p>
  
  <p>I have tried this using if condition but it is not working:</p>
  
  <pre><code>child.sendline('command1')
  i=child.expect('[#\$] ')
  if i:
      child.sendline('command2')
      child.expect('[#\$] ')
  else:
      print "NULL NULL"
  </code></pre>
  
  <p>But this is not working.</p>
  
  <p>How to capture the return codes of pexpect and how we will validate those values?</p>