LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2548|回复: 0

python: pexpect 调用 ssh 进行远程登陆?

[复制链接]
发表于 2006-3-29 14:50:14 | 显示全部楼层 |阅读模式
想写一个 python 脚本,实现 ssh 命令的功能,可以使用 pexpect 模块、paramiko 模块或 public key 来实现远程登陆和执行命令(在参数中指定),并且可以在参数中直接指定密码来实现批处理。

目前做了一下用 pexpect 的方法,远程执行命令没有问题,但如果仅仅是要远程登陆就不行,总是超时退出,请问有什么办法没有?

  1. #!/usr/bin/python
  2. # -*- encoding: UTF-8 -*-

  3. import re
  4. import sys
  5. import time
  6. import pexpect
  7. import getopt

  8. class pyssh:
  9.         def __init__(self, user='nobody', passwd='', host='localhost', method='pexpect', info=None):
  10.                 self.user = user
  11.                 self.host = host
  12.                 self.passwd = passwd
  13.                 self.method = method
  14.                 if info:
  15.                         try:
  16.                                 self.user = info['user']
  17.                                 self.host = info['host']
  18.                                 self.passwd = info['passwd']
  19.                                 self.method = info['method']
  20.                         except KeyError, (errno, strerror):
  21.                                 print 'Not enough infomations.'
  22.                                 sys.exit(1)

  23.         def rexec(self, command='', timeout=60):
  24.                 if self.method == 'pexpect':
  25.                         if type(command) is not str:
  26.                                 raise 'Not a command string.'

  27.                         if command:  
  28.                                 command = 'ssh %s@%s "%s"' % (self.user, self.host, command)
  29.                         else:
  30.                                 command = 'ssh %s@%s' % (self.user, self.host)
  31.                         print command
  32.                         T = time.time()
  33.                         try:
  34.                                 ssh = pexpect.spawn(command, timeout=timeout)
  35.                                 idx = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
  36.                                 if idx == 0: ssh.sendline(self.passwd)
  37.                                 elif idx == 1:  ssh.sendline('yes')
  38.                         except pexpect.EOF:
  39.                                 ssh.close()
  40.                                 print '%d seconds' % int(time.time() - T)
  41.                                 return 1
  42.                         # except pexpect.TIMEOUT:
  43.                         print ssh.read()
  44.                         ssh.expect(pexpect.EOF)
  45.                         ssh.close()
  46.                         print '%d seconds' % int(time.time() - T)

  47. # class ssh_pexpect(pyssh):

  48. def usage():
  49.         print 'usage'

  50. if __name__ == '__main__':
  51.         try:
  52.                 opts, args = getopt.getopt(sys.argv[1:], 'a:u:p:h:m:t:')
  53.         except getopt.GetoptError, (errno, strerror):
  54.                 print 'getopt error: %d, %s' % (errno, strerror)
  55.                 usage()
  56.                 sys.exit(2)

  57.         optmap = {'method' : 'pexpect'}
  58.         timeout = 30
  59.         for opt, value in opts:
  60.                 if opt == '-a':
  61.                         try:
  62.                                 optmap['host'] = value.split('@')[1]
  63.                                 uptmp = value.split('@')[0]
  64.                                 optmap['user'] = uptmp.split(':')[0]
  65.                                 optmap['passwd'] = uptmp.split(':')[1]
  66.                         except IndexError:
  67.                                 print 'Lack of info'
  68.                                 sys.exit(3)
  69.                 if opt == '-u':  optmap['user'] = value
  70.                 if opt == '-h':  optmap['host'] = value
  71.                 if opt == '-p':  optmap['passwd'] = value
  72.                 if opt == '-m':  optmap['method'] = value
  73.                 if opt == '-t':  timeout = value

  74.         if not args:
  75.                 ssh = pyssh(info=optmap)
  76.                 ssh.rexec(timeout=timeout)
  77.         else:
  78.                 ssh = pyssh(info=optmap)
  79.                 command = args[0]
  80.                 ssh.rexec(command, timeout=timeout)
复制代码


谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表