简体   繁体   English

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

[英]Cannot authenticate to SSH server with correct credentials (username and password) read from a local file

I'm making a small program that connects into an SSH server, testing multiple usernames and passwords that are inside a .txt file. 我正在制作一个连接到SSH服务器的小程序,测试.txt文件中的多个用户名和密码。 The problem I'm having is when it get to the correct username and password it doesn't connect, or at least my if statement doesn't properly say it. 我遇到的问题是当它找到正确的用户名和密码时它没有连接,或者至少我的if语句没有正确地说出来。

If I do pass it as regular variable or just set the username = 'correct user' ,password = 'correct password' , it works. 如果我将它作为常规变量传递或只是设置username = 'correct user' ,password = 'correct password' ,它就可以了。 Here's part of the code, I'm testing in a local environment with a Linux VM: 这是代码的一部分,我正在使用Linux VM在本地环境中进行测试:

# 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')

When you use readlines , it keeps trailing new line characters in the lines. 当您使用readlines ,它会在行中保留尾随新行字符。

So you are actually connecting with wrong credentials as both your username and password contain new line, which is not a part of the correct credentials. 因此,您实际上是使用错误的凭据进行连接,因为您的用户名和密码都包含新行,这不是正确凭据的一部分。

See Getting rid of \\n when using .readlines() . 请参阅使用.readlines()时删除\\ n

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文本文件中读取用户名和密码 - read username and password from text file 在Userlogin期间,Authenticate返回None,用户名和密码正确 - Authenticate returns None with correct username and password during Userlogin Tweepy使用用户名/密码进行身份验证 - Tweepy authenticate with username/password 如何循环输入密码/用户名登录页面并从外部文件读取密码/用户名? - How do I loop my password/username login page and read the password/username from an external file? 使用Python使用SSH从服务器读取文件 - Read a file from server with SSH using Python Python ssh到具有用户名和密码的服务器,仅使用子进程登录 - Python ssh to a server having a username and password to login using subprocess only 当从文件输入正确的密码和用户名时,break函数不起作用 - break function does not work when the correct password and username are entered from the file Paramiko SFTP 不会使用正确的用户名和密码进行身份验证,但 GUI SFTP 客户端会通过键盘交互式身份验证成功 - Paramiko SFTP will not authenticate with correct username and password but GUI SFTP client success with keyboard-interactive authentication 从我用来存储登录用户名和密码的文本文件顶部读取 - Read from top of text file that I use to store login username and password 使用python将文件从本地发送到服务器-正确的方法 - Sending a file from local to server using python - the correct way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM