pexpect实现的ssh连接(pexpect可从sourceforge下载)

  1. def ssh_cmd(ippasswdcmdnewpwd):
  2.     ret = -1
  3.     ssh = pexpect.spawn('ssh -q -p36000 root@%s "%s"' % (ipcmd))
  4.     try:
  5.         i = ssh.expect(['password: ''continue connecting (yes/no)?']timeout=5)
  6.         if i == 0 :
  7.             ssh.sendline(passwd)
  8.         elif i == 1:
  9.             ssh.sendline('yes\n')
  10.             ssh.expect('password: ')
  11.             ssh.sendline(passwd)
  12.         ssh.expect('New Password:')
  13.         ssh.sendline(newpwd)
  14.         ssh.expect('Reenter New Password:')
  15.         ssh.sendline(newpwd)
  16.         r = ssh.read()
  17.         ret = 0
  18.     except pexpect.EOF:
  19.         print "EOF"
  20.         ssh.close()
  21.         ret = -1
  22.     except pexpect.TIMEOUT:
  23.         print "TIMEOUT"
  24.         ssh.close()
  25.         ret = -2
  26.     return ret
  27. 参考:http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/

转载于:https://www.cnblogs.com/cursorkey/archive/2011/08/02/2125635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值