[英]Python Pexpect Attribute error. 'NoneType' has no attribute 'sendline'
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()
[-] 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'
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.