zoukankan      html  css  js  c++  java
  • 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/
  • 相关阅读:
    STM32-串口通信
    STM32-系统计时器(systick)
    字符串操作常用的函数
    基本MarkDown语法
    结构
    python入门
    贪心算法小结
    POJ1631_高深DP
    POJ3046ANT_COUNTING
    POJ1742coins
  • 原文地址:https://www.cnblogs.com/cursorkey/p/2125635.html
Copyright © 2011-2022 走看看