3

I'm making a small program that connects into an SSH server, testing multiple usernames and passwords that are inside a .txt file. 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 I do pass it as regular variable or just set the username = 'correct user' ,password = 'correct password', it works. Here's part of the code, I'm testing in a local environment with a 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')
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Vitor
  • 43
  • 2
  • 7
  • What is your output? You have a nested `for`. This means you want to try every username with every password. Is that what you intended? – h4z3 Sep 02 '19 at 12:13
  • Yes, that's what i wanted, the output is always 'User and/or password is incorrect', until it finishes with an error. If I do put, manually, the correct user and password it gets inside the 'YES' – Vitor Sep 02 '19 at 12:46
  • Have you tried printing the usernames/passwords you get? Maybe there's some whitespace or other stuff that shouldn't be there? Or maybe you're getting rate-limited and after a few failed attempts, it just blocks you from trying again? – h4z3 Sep 02 '19 at 12:48
  • When I print it, it shows the correct user and password, doesn't seem to be a white space in it. it does seem like i'm getting limited, but if I put the correct user and pass in the first position of the file, it still doesn't work. Could it be the breaking of the line in the file? – Vitor Sep 02 '19 at 12:50
  • Try printing a formatted text inside quotations, so you can see whether there's a trailing space or something - `print('"{}"'.format(user))` (+ the same for password). Or just try passing `user.strip()` and `psswd.strip()`, so it any whitespace from beginning or end of the string gets cut of. – h4z3 Sep 02 '19 at 13:08

1 Answers1

2

When you use readlines, it keeps trailing new line characters in the lines.

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().

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Oh. Interesting! I always iterate over the lines and because of that I've always thought `readlines` returns the same elements in a list. :o – h4z3 Sep 02 '19 at 13:09
  • Thanks Martin, can't test at the moment, but as soon as I'm back home, I'll test it, pretty sure that was the problem though, when h4z3 mentioned it, it clicked. – Vitor Sep 02 '19 at 14:42