37,721
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import pexpect
import getpass, os
def ssh_command(user, host, password, command):
ssh_newkey="Are you sure you want to continue connecting"
child = pexpect.spawn ('ssh -l %s %s %s'%(user, host, command))
i=child.expect([pexpect.TIMEOUT, ssh_newkey, 'passowrd: '])
if i==0:
print 'ERROR! 1'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
# 程序执行到这,返回None
return None
def main():
host = raw_input('Hostname: ')
user = raw_input('User: ')
password = getpass.getpass()
command = raw_input('Enter the command: ')
# 此时child为None值
child = ssh_command(user, host, password, command)
# 抛出异常
child.expect(pexpect.EOF)
print child.before
if __name__ == '__main__':
try:
main()
except Exception, e:
print str(e)
# traceback.print_exc()
os._exit(1)