Last active
August 29, 2015 13:57
-
-
Save upbit/9659121 to your computer and use it in GitHub Desktop.
python-pexpect demo
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
import pexpect # download from https://github.com/pexpect/pexpect | |
def remote_ssh_cmd(ip, cmd, user="root", passwd=""): | |
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd)) | |
r = '' | |
try: | |
i = ssh.expect(['password: ', 'continue connecting (yes/no)?']) | |
if i == 0 : | |
ssh.sendline(passwd) | |
elif i == 1: | |
ssh.sendline('yes') | |
except pexpect.EOF: | |
ssh.close() | |
else: | |
print '>> ssh %s@%s "%s"' % (user, ip, cmd) | |
r = ssh.read() | |
ssh.expect(pexpect.EOF) | |
ssh.close() | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment