Skip to content

Instantly share code, notes, and snippets.

@OneGneissGuy
Last active March 15, 2017 18:16
Show Gist options
  • Save OneGneissGuy/c7a14faf5c9c93d5a3681c42ece9ead3 to your computer and use it in GitHub Desktop.
Save OneGneissGuy/c7a14faf5c9c93d5a3681c42ece9ead3 to your computer and use it in GitHub Desktop.
driver for ssh automation using pexpect
#v1.0 - to supply password and login
#shamlessly stolen form the web
import sys
import pexpect
user = 'user'
password = 'password'
host = 'host1.us.com'
command = 'hostname ; echo $?'
def dossh(user, password, host, command):
child = pexpect.spawn('ssh %s@%s %s' % (user,host,command),logfile=sys.stdout,timeout=None)
prompt = child.expect(['password:', r"yes/no",pexpect.EOF])
if prompt == 0:
child.sendline(password)
elif prompt == 1:
child.sendline("yes")
child.expect("password:", timeout=30)
child.sendline(password)
data = child.read()
print data
child.close()
dossh(user, password, host, command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment