【发布时间】:2024-07-31 06:05:02
【问题描述】:
def connect(user,host,keyfile,release):
global Stop
global Fails
try:
perm_denied = 'Permission denied'
ssh_newkey = 'Are you sure you want to continue'
conn_closed = 'Connection closed by remote host'
opt = ' -o PasswordAuthentication=no'
connStr= 'ssh ' + user + '@' + host + ' -i ' +keyfile + opt
child = pexpect.spawn(connStr)
ret=child.expect([pexpect.TIMEOUT,perm_denied,ssh_newkey,conn_closed,'$','#'])
print(child.before)
if ret== 2:
print('[[-] Adding Host to !/.ssh/known_hosts')
child.sendline('yes')
elif ret ==3:
print('[-] Connection Closed by Remote Host')
Fails += 1
elif ret > 3:
print('[+] Success.' + str(keyfile)+ ' ' + str(ret))
Stop = True
finally:
if release:
connection_lock.release()**
请检查我上面的python代码。
当我执行时:
python3 brutekey-ssh.py -H 127.0.0.1 -u root -d dsa/1024/
[-] Testing keyfile dsa/1024/a31b082ec6434d65c2adf76862b9aca7-30343
[-] Testing keyfile dsa/1024/fb80119b7615bbeb96cb7d2f55b7533d-10375
b''
[+] Success.dsa/1024/1f09490e311786ec22ff32715ca106e9-1279 4
[*] Exiting:Key Found
b''
[+] Success.dsa/1024/b23696eee5b31ed916002d3ec2ddb5f6-18108 4
b''
[+] Success.dsa/1024/a31b082ec6434d65c2adf76862b9aca7-30343 4
我的问题如下:
即使权限被拒绝,它仍然匹配
ret > 3,为什么?如何检查
child.expect的准确输出我是否需要使用
.*\$而不是$?$是否只匹配输出中的确切$?
【问题讨论】:
-
你在 perm_denied 中给出了什么字符串?
-
在
child.before这个语句后面加上print值ret = child.expect([pexpect.TIMEOUT,perm_denied,ssh_newkey,conn_closed,'$','#'])这样我们就可以知道问题的确切原因了。 -
Errr 你想做什么?如果您必须将密码/用户名传递给期望在命令行上输入这些输入的阻塞进程,则通常需要
expect。但是,您正在使用密钥进行 ssh-ing,因此您不需要这样做... -
此外,通常从进程返回代码
0意味着在基于 Unix 的系统上成功。大于 3 的返回码可能意味着任何事情......但不是成功。 -
期望不返回进程代码。它返回索引