Skip to content

Instantly share code, notes, and snippets.

@upbit
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upbit/9659121 to your computer and use it in GitHub Desktop.
Save upbit/9659121 to your computer and use it in GitHub Desktop.
python-pexpect demo
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