简体   繁体   English

Python Pexpect属性错误。 'NoneType'没有属性'sendline'

[英]Python Pexpect Attribute error. 'NoneType' has no attribute 'sendline'

Writing a script using Pexpect to connect via ssh but it is throwing an attribute error. 使用Pexpect编写脚本以通过ssh进行连接但是它会抛出属性错误。

import pexpect
PROMPT = ['# ', '>>> ', '> ', '\$ ', '~# ']
def send_command(child, cmd):
    child.sendline(cmd)
    child.expect(PROMPT)
    print child.before, child.after
def connect(user, host, password):
    ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
    connStr = 'ssh ' + user + '@' + host
    child = pexpect.spawn(connStr)
    ret = child.expect([ssh_newkey, 'password:'])
    if ret == 0:
        print '[-] Error Connecting'
        return
    elif ret == 1:
        child.sendline('yes')
        ret = child.expect('password:')
        if ret == 0:
            print '[-] Error Connecting'
            return
    child.sendline(password)
    child.expect(PROMPT)
    return child
def main():
    host = 'test.rebex.net'
    user = 'demo'
    password = 'password'
    child = connect(user, host, password)
    send_command(child, 'cat /etc/shadow | grep root')
if __name__ == '__main__':
    main()

I am getting the following error: 我收到以下错误:

[-] Error Connecting
Traceback (most recent call last):
  File "./bruteSSH.py", line 33, in <module>
    main()
  File "./bruteSSH.py", line 31, in main
    send_command(child, 'cat /etc/shadow | grep root')
  File "./bruteSSH.py", line 6, in send_command
    child.sendline(cmd)
AttributeError: 'NoneType' object has no attribute 'sendline'

I believe it has something to do with my child object being a 'NoneType' but I am not able to pin down what I am doing wrong. 我认为它与我的孩子对象是'NoneType'有关,但是我无法确定我做错了什么。

First of all your indentation is wrong on the 6 line. 首先你的缩进在6行是错误的。

It's causing this error because the child object has not been setup yet properly and connected successfully. 它导致此错误,因为尚未正确设置子对象并成功连接。

If this is exactly your code then the problem is that "child.sendline()" is executed outside the function whereas child is a local variable inside the function "send_command" so globally the child variable has not yet been defined 如果这正是你的代码,那么问题是“child.sendline()”在函数外执行,而child是函数“send_command”中的局部变量,所以全局的子变量尚未定义

You don't return a value on a couple of conditions. 您不会在几个条件下返回值。 That's where you're getting your None and what is causing your error. 这就是你得到你的无和导致错误的原因。 See the commented lines below: 请参阅下面的注释行:

if ret == 0:
    print '[-] Error Connecting'
    return  # THIS WILL CAUSE YOUR ERROR

elif ret == 1:
    child.sendline('yes')
    ret = child.expect('password:')
    if ret == 0:
        print '[-] Error Connecting'
        return  # THIS WILL ALSO CAUSE YOUR ERROR

But your logic is flawed anyway. 但无论如何,你的逻辑是有缺陷的。 Expect returns a 0 or the index of the match if you pass it an array. 如果将数组传递给Expect,则返回0或匹配的索引。 In your code, your passing it an array. 在你的代码中,你传递一个数组。 So a return value of a 0 indicates that it successfully matched your first entry-- the "Are you sure" condition. 因此,返回值为0表示它成功匹配您的第一个条目 - “您确定”条件。 If you match that you'd want to send the "yes". 如果你匹配,你想发送“是”。 Below is more what I think you're after... 以下是我认为你追求的更多......

import pexpect
PROMPT = ['# ', '>>> ', '> ', '\$ ', '~# ']
def send_command(child, cmd):
    child.sendline(cmd)
    child.expect(PROMPT)
    print child.before, child.after

def connect(user, host, password):
    ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
    connStr = 'ssh ' + user + '@' + host
    child = pexpect.spawn(connStr)
    ret = child.expect(['password:', ssh_newkey])
    if ret == 1:
        child.sendline('yes')
        ret = child.expect('password:')
    if ret != 0:
        print '[-] Error Connecting'
        return  # THIS WILL RETURN A NONE SO YOU SHOULD CHECK FOR IT.  SHOULD EXPLICITLY DO A return None TO MAKE IT CLEARER
    child.sendline(password)
    child.expect(PROMPT)
    return child

def main():
    host = 'localhost'
    user = 'demo'
    password = 'password'
    child = connect(user, host, password)
    if child is not None:
        send_command(child, 'cat /etc/shadow | grep root')
    else:
        print "Problem connecting!"

if __name__ == '__main__':
    main()

The problem is right in front of you. 问题就在你面前。 When you encounter an error in the connect function as you are as shown by the "[*] Error Connection" print statement, you return nothing. 当您在连接函数中遇到错误时,如“[*]错误连接”打印语句所示,您什么都不返回。 Only if the connection was successful it returns the child object, but as the connection failed you return a "Null Object" and exit out of your function. 只有连接成功,它才会返回子对象,但是当连接失败时,返回“Null Object”并退出函数。 You are not able to make a successful connection and hence the child object is never returned to your "child" Variable in your main Function. 您无法成功建立连接,因此子对象永远不会返回到主函数中的“子”变量。 And you pass this same "Null Object" to your send_command() and hence does not work 并将此相同的“Null对象”传递给send_command(),因此不起作用


import sys

def connect(user, host, password):
    ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
    connStr = 'ssh ' + user + '@' + host
    child = pexpect.spawn(connStr)
    ret = child.expect([ssh_newkey, 'password:'])
    if ret == 0:
        print '[-] Error Connecting'
        sys.exit()
    elif ret == 1:
        child.sendline('yes')
        ret = child.expect('password:')
        if ret == 0:
            print '[-] Error Connecting'
            sys.exit()
    child.sendline(password)
    child.expect(PROMPT)
    return child

Now your program will only proceed if the connection was successful. 现在,只有连接成功,您的程序才会继续。 Maybe the expects and password maybe wrong, the overall problem is you are not able to make a successful connection 也许预期和密码可能是错误的,整体问题是你无法成功连接

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

相关问题 NoneType' object 没有属性 'group' 错误。 Python代码 - NoneType' object has no attribute 'group' error. Python code Python-属性错误,“ NoneType”对象没有属性 - Python - Attribute error, 'NoneType' object has no attribute AttributeError:&#39;NoneType&#39;对象没有属性&#39;sendline&#39;,但是模块包含对该属性进行另一种测试的属性? - AttributeError: 'NoneType' object has no attribute 'sendline' yet module contains the attribute having tested it another way? “NoneType”对象没有被pythonlinkedinapi属性错误 - "NoneType" Object has no attribute error by python linkedinapi Python错误&#39;NoneType&#39;对象没有属性&#39;&lt;...&gt;&#39; - Python error 'NoneType' object has no attribute '<…>' Python:另一个“ NoneType”对象没有属性错误 - Python : Another 'NoneType' object has no attribute error Python“AttributeError:&#39;NoneType&#39;对象没有属性”错误 - Python "AttributeError: 'NoneType' object has no attribute" Error Python错误; &#39;NoneType&#39;对象没有属性&#39;after&#39; - Python error; 'NoneType' object has no attribute 'after' Python:属性错误Nonetype对象没有属性&#39;nxt&#39; - Python: Attribute error Nonetype object has no attribute 'nxt' 属性错误:“NoneType”object 在 Python 中没有属性“光标” - Attribute Error: 'NoneType' object has no attribute 'cursor' in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM