[英]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.