automation using python

Im trying to write an automation script using python. I expect this script to log in to a remote server, execute commands and get its output.

import pexpect 
child=pexpect.spawn('ssh myuser@192.168.151.80')
child.expect('Password:')
child.sendline('mypassword')
get_output = child.sendline('uname')
if get_output == 'Solaris':
  print 'this is solaris server'
else:
  print 'this is some other machine'

The fifth line of the code is wrong. Please let me know how to achieve this, I want to get the output of the command sent.

Also if the output is more than one line, how to store the output?
Is there any other better python module for automatio/remote execution ?

The reason you have to use a third-party brute-forcing utility to kludge stored plaintext passwords into ssh is because stored passwords are such a bad idea, ssh is actually designed to prevent you from using them. So are most other login systems.

If you used ssh as it was intended, with ssh keys, your job would be much simpler.

NAME=`ssh username@host uname`

Corona688: Im able to login to the remote server and execute the commands. Problem is, im unable to get the output of the commands executed.

Yes, because you're using such a complicated scheme. Using ssh as intended, getting its output is trivial. You can even send entire scripts over the wire with a simple redirect instead of using third-party brute forcing tools like expect.

Finally found the solution... Heres the code..

import pexpect, datetime
child=pexpect.spawn('ssh myuser@192.168.151.80')
child.expect('Password:')
child.sendline('mypassword')
time.sleep(2)
child.sendline('uname')
get_output = child.before.split('\n')[-1]
if get_output == 'Solaris':
  print 'this is solaris server'
else:
  print 'this is some other machine'