简体   繁体   English

ssh密码错误时pxssh中断

[英]pxssh Breaks When ssh Password Is Incorrect

I Am Trying To Connect to an ssh server in python using pxssh lib 我正在尝试使用pxssh lib连接到python中的ssh服务器

#!/usr/bin/python

import pxssh

attempt = pxssh.pxssh()

if not attempt.login(IP, USERNAME, PASSWORD):

    print("SSH LOGIN FAILED USING USERNAME : " + USERNAME + " AND PASSWORD : " + PASSWORD)

else:

    print("LOGIN WAS SUCCESSFUL USING USERNAME : " + USERNAME + " AND PASSWORD : " + PASSWORD)

    attempt.sendline("pwd")

    attempt.prompt()

    print attempt.before

break

it works fine when password is correct and logs into server and executes 当密码正确并登录服务器并执行时,它可以正常工作

pwd

but when password is incorrect or login failes for any reason the program breaks i expect this : 但是当密码不正确或由于任何原因登录失败时,程序中断,我希望这样:

print("SSH LOGIN FAILED USING USERNAME : " + USERNAME + " AND PASSWORD : " + PASSWORD)

but instead i get this error: 但是我却得到了这个错误:

  File "./ssh.py", line 19, in <module>
    if not attempt.login(IP, USERNAME, PASSWORD):
  File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 292, in login
    raise ExceptionPxssh ('password refused')
pexpect.pxssh.ExceptionPxssh: password refused

If it raises an exception when the password is incorrect, then you have to catch the exception rather than rely on a return value. 如果密码不正确时引发异常,则必须捕获异常而不是依赖返回值。

Return values and exceptions are two totally different paradigms in dealing with potential error conditions. 返回值和异常是处理潜在错误条件的两种完全不同的范例。

As to how to catch exceptions, see https://docs.python.org/3.5/tutorial/errors.html for the canonical guide. 关于如何捕获异常,请参阅https://docs.python.org/3.5/tutorial/errors.html以获取规范指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM