LinuxSir.cn,穿越时空的Linuxsir!

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

python: pexpect "OSError: [Errno 5] Input/output error"

[复制链接]
发表于 2006-3-3 10:06:44 | 显示全部楼层 |阅读模式
求教,使用 pexpect 结果的情况如下,不知道是什么原因:

>>> foo = pexpect.spawn('scp root@192.168.126.11 /tmp')
>>> foo.logfile = sys.stdout
>>> foo.expect('.*password:')
root@192.168.126.11's password: 0
>>> foo.sendline('symqfct')  
symqfct
8
>>> foo.interact()


expect-5.42.1-1.i386.rpm                      100%  148KB 147.7KB/s   00:00   

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/site-packages/pexpect.py", line 1062, in interact
    self.__interact_copy(escape_character)
  File "/usr/lib/python2.3/site-packages/pexpect.py", line 1086, in __interact_copy
    data = self.__interact_read(self.child_fd)
  File "/usr/lib/python2.3/site-packages/pexpect.py", line 1075, in __interact_read
    return os.read(fd, 1000)
OSError: [Errno 5] Input/output error

谢谢
发表于 2006-3-3 11:32:43 | 显示全部楼层
你用SSH协议不是可以用证书登录吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-3 13:03:24 | 显示全部楼层
那么在 python 中如何实现呢?我试过 paramiko 模块,但不太方便,而且在传输大量文件时,中间挂起。
回复 支持 反对

使用道具 举报

发表于 2006-3-3 22:40:52 | 显示全部楼层
你最终想实现什么目的呢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-6 13:06:29 | 显示全部楼层
最终就是要实现能够定时使用 ssh 从远端取得文件,比如日志,每小时或每天。但是需要使用 python。

我现在的实现如下:

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

  3. import os
  4. import time
  5. import pexpect
  6. import tarfile

  7. class ft_handler:
  8.         def __init__(self, info):
  9.                 self.user = info['user']
  10.                 self.host = info['host']
  11.                 self.passwd = info['passwd']
  12.                 self.method = info['method']
  13.                 self.regexp = info['regexp']
  14.                 self.srcdir = info['srcdir']
  15.                 self.dstdir = info['dstdir']
  16.                 self.files = info['files']

  17.         def getfile(self, srcdir = '', dstdir = '', files = ''):
  18.                 if srcdir:      self.srcdir = srcdir
  19.                 if dstdir:      self.dstdir = dstdir
  20.                 if files:       self.files = files

  21.                 if self.method == 'ssh.pexpect':
  22.                         cmd = 'ssh %s@%s "cd %s; find . | grep %s >tmplist; tar -T tmplist -c -z -f tmp.tgz"' \
  23.                                 % (self.user, self.host, self.srcdir, self.files)
  24.                         self.ssh_pexpect(cmd, 300)

  25.                         if not os.path.isdir(self.dstdir):  os.makedirs(self.dstdir)
  26.                         cmd = 'scp %s@%s:%s/tmp.tgz %s/' % (self.user, self.host, self.srcdir, self.dstdir)
  27.                         self.ssh_pexpect(cmd, 600)

  28.                         cmd = 'ssh %s@%s "cd %s; rm tmplist tmp.tgz"' % (self.user, self.host, self.srcdir)
  29.                         self.ssh_pexpect(cmd)

  30.                         tar = tarfile.open('%s/tmp.tgz' % self.dstdir, 'r:gz')
  31.                         for T in tar:  tar.extract(T, self.dstdir)

  32.                         os.remove('%s/tmp.tgz' % self.dstdir)

  33.                 # elif self.method == 'ssh.paramiko':

  34.         def ssh_pexpect(self, cmd, timeout=30):
  35.                 print cmd
  36.                 T = time.time()
  37.                 ssh = pexpect.spawn(cmd, timeout=timeout)
  38.                 ssh.expect(['password: '])
  39.                 ssh.sendline(self.passwd)
  40.                 ssh.expect(pexpect.EOF)
  41.                 # ssh.sendeof()
  42.                 ssh.close()
  43.                 print '%d seconds' % int(time.time() - T)
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-6 13:19:56 | 显示全部楼层
最后使用一个 expect(pexect.EOF),而不是使用 interact()。不过这里有一个超时的问题,超时后会抛出异常并终止。
回复 支持 反对

使用道具 举报

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

本版积分规则

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