pexpect调用ssh的报错:'NoneType' object has no attribute 'expect'
本人初学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)
遇到报错,无法解决。望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)
...全文
相关推荐
python pexpect ssh 远程登录服务器的方法
本文介绍如何使用Python的pexpect模块进行SSH远程登录服务器,包括模块的检查、安装,以及编写测试代码实现自动登录和执行命令。在Python编程中,pexpect模块是一个非常有用的库
Python实现ssh批量登录并执行命令
('ssh root@{} "{}"'.format(ip, cmd)) try: i = ssh.expect(['password:', 'continue connecting (yes/no)?
python之pexpect实现自动交互的例子
**pexpect的基本概念** pexpect是一个Python实现的Expect库,Expect是一种用于自动化交互式应用程序的工具,而pexpect则将其功能带入了Python世界。
python Pexpect 实现输密码 scp 拷贝的方法
如果不调用`read()`,可能在数据传输完成前程序就已经结束,导致文件拷贝失败。Pexpect库提供了丰富的功能,如匹配正则表达式、处理异常情况以及设置超时等。
Python pexpect模块及shell脚本except原理解析
`interact`:允许用户与子进程进行交互。在自动化过程中,如果需要手动介入,可以调用`interact`将控制权交还给用户。


