Last active
March 15, 2017 18:16
-
-
Save OneGneissGuy/c7a14faf5c9c93d5a3681c42ece9ead3 to your computer and use it in GitHub Desktop.
driver for ssh automation using pexpect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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