Python 预计属性错误。 'NoneType' 没有属性 'sendline'
Python Pexpect Attribute error. 'NoneType' has no attribute 'sendline'
正在使用 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()
我收到以下错误:
[-] 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'
我认为这与我的子对象是 'NoneType' 有关,但我无法确定我做错了什么。
首先你在第 6 行的缩进是错误的。
导致此错误的原因是子对象尚未正确设置和连接成功。
如果这正是您的代码,那么问题是 "child.sendline()" 在函数外部执行,而 child 是函数内部的局部变量 "send_command"
所以在全球范围内,子变量尚未定义
您 return 有几个条件没有值。这就是您获得 None 的地方以及导致错误的原因。请参阅下面的注释行:
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
但无论如何你的逻辑是有缺陷的。如果将数组传递给它,则期望 returns 为 0 或匹配项的索引。在您的代码中,您将一个数组传递给它。因此,return 值为 0 表示它成功匹配您的第一个条目 - "Are you sure" 条件。如果你匹配,你想发送 "yes"。以下是我认为您想要的更多内容...
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()
问题就在你面前。当您在连接函数中遇到“[*] 错误连接”打印语句所示的错误时,您 return 什么都没有。仅当连接成功时,它 return 才是子对象,但当连接失败时,您会 return 一个 "Null Object" 并退出您的函数。您无法建立成功的连接,因此子对象永远不会 returned 到您的主函数中的 "child" 变量。
并且您将相同的 "Null Object" 传递给您的 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
现在您的程序只有在连接成功时才会继续。
可能期望和密码错误,总体问题是无法成功连接
正在使用 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()
我收到以下错误:
[-] 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'
我认为这与我的子对象是 'NoneType' 有关,但我无法确定我做错了什么。
首先你在第 6 行的缩进是错误的。
导致此错误的原因是子对象尚未正确设置和连接成功。
如果这正是您的代码,那么问题是 "child.sendline()" 在函数外部执行,而 child 是函数内部的局部变量 "send_command" 所以在全球范围内,子变量尚未定义
您 return 有几个条件没有值。这就是您获得 None 的地方以及导致错误的原因。请参阅下面的注释行:
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
但无论如何你的逻辑是有缺陷的。如果将数组传递给它,则期望 returns 为 0 或匹配项的索引。在您的代码中,您将一个数组传递给它。因此,return 值为 0 表示它成功匹配您的第一个条目 - "Are you sure" 条件。如果你匹配,你想发送 "yes"。以下是我认为您想要的更多内容...
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()
问题就在你面前。当您在连接函数中遇到“[*] 错误连接”打印语句所示的错误时,您 return 什么都没有。仅当连接成功时,它 return 才是子对象,但当连接失败时,您会 return 一个 "Null Object" 并退出您的函数。您无法建立成功的连接,因此子对象永远不会 returned 到您的主函数中的 "child" 变量。 并且您将相同的 "Null Object" 传递给您的 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
现在您的程序只有在连接成功时才会继续。 可能期望和密码错误,总体问题是无法成功连接