无法使用从本地fi读取的正确凭据(用户名和密码)对SSH服务器进行身份验证

2024-04-16 05:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个连接到SSH服务器的小程序,测试一个.txt文件中的多个用户名和密码。我遇到的问题是,当它得到正确的用户名和密码时,它无法连接,或者至少我的if语句不能正确地说出来。你知道吗

如果我把它作为正则变量传递,或者只是设置username = 'correct user' ,password = 'correct password',它就可以工作了。下面是部分代码,我正在使用Linux虚拟机在本地环境中进行测试:

# read files
fs = open('users.txt','r')
users = fs.readlines()
fs.close()
fs = open('passwords.txt','r')
psswds = fs.readlines()
fs.close()

# connect and test the password
for user in users:
    for psswd in psswds:
        try:        
            print(usuario)
            print(senha)
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            con = ssh.connect(ip,port,username = user,password = psswd)
            if con is None:
                print ('yes')
                break
            else:
                print ('nop')
            SSHClient.close()
        except paramiko.AuthenticationException:
            print('User and/or password incorrect')

Tags: txtparamiko密码closeifusernamepasswordopen
1条回答
网友
1楼 · 发布于 2024-04-16 05:19:13

当您使用readlines时,它会在行中保留新行字符。你知道吗

因此,您实际上使用了错误的凭据进行连接,因为您的用户名和密码都包含新行,这不是正确凭据的一部分。你知道吗

Getting rid of \n when using .readlines()。你知道吗

相关问题 更多 >