pexpect调用ssh的报错:'NoneType' object has no attribute 'expect'

Steel_Victor 2011-10-15 12:43:07
本人初学python,是菜鸟中的菜鸟。今天跑了一个脚本,调用pexpect连接ssh到远端服务器运行命令并返回结果。python版本是2.5.
遇到报错,无法解决。望DX赐教。谢谢!

错误信息(斜体字部分是脚本中定义的部分信息):
ERROR! 1
SSH could not login. Here is what SSH said:

Password: <class 'pexpect.TIMEOUT'>
'NoneType' object has no attribute 'expect'

这个报错是说对象未定义类型?错误应该是发生在第一个if i==0处,但是child已经是由pexpect.spawn生成的对象了,为什么要报错说NoneType?



完整脚本:(来源: http://www.oschina.net/question/12_7583 )

#!/usr/bin/env python
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
return None
if i==1:
child.sendline('yes')
child.expect('password: ')
i=child.expect([pexpect.TIMEOUT, 'password: '])
if i==0:
print 'ERROR! 2'
print 'SSH could not login. here is what SSH said:'
print child.before, child.after
return None
child.sendline(password)
return child

def main():
host = raw_input('Hostname: ')
user = raw_input('User: ')
password = getpass.getpass()
command = raw_input('Enter the command: ')
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)

...全文
462 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hentianxu 2013-04-27
  • 打赏
  • 举报
回复
加个超时参数就行,默认30S超时,估计你的网络不太好吧。 i=child.expect([pexpect.TIMEOUT, ssh_newkey, 'passowrd: '],timeout=180)
Steel_Victor 2011-11-02
  • 打赏
  • 举报
回复
我补充一下,我用这个脚本对一台安装了ssh服务的windows服务器做操作,是正常。但对另外一台负载均和设备就会失败。直接用ssh命令连接,又是正常。
有点头大。。。
Steel_Victor 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 userguanguan 的回复:]

Python code

#!/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 conn……
[/Quote]
谢谢大侠! 请帮忙看一下我刚才的回复。为什么直接运行ssh -l命令结果是正常的,但用这个脚本就是不行?
Steel_Victor 2011-11-02
  • 打赏
  • 举报
回复
谢谢大侠!不过我直接用ssh -l user host command测试那台host,是可以的。用这个脚本就会报错。很奇怪。
这是什么原因?
荷兰人号 2011-10-19
  • 打赏
  • 举报
回复
child = pexpect.spawn ('ssh -l %s %s %s'%(user, host, command))
i=child.expect([pexpect.TIMEOUT, ssh_newkey, 'passowrd: '])

从错误来看,是ssh超时了,导致child是none的
直接用none的expect函数报了下面的错误'NoneType' object has no attribute 'expect'

所以,先确认下是否ssh能连上
,第二加个try
livesguan 2011-10-16
  • 打赏
  • 举报
回复

#!/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)



希望对楼主有所帮助...

建议楼主,插入代码一定要记得缩进..

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧