Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUPPORT] - SSH Authentication Failed #2378

Open
raspberrypigpi0 opened this issue Apr 3, 2024 · 0 comments
Open

[SUPPORT] - SSH Authentication Failed #2378

raspberrypigpi0 opened this issue Apr 3, 2024 · 0 comments
Labels

Comments

@raspberrypigpi0
Copy link

raspberrypigpi0 commented Apr 3, 2024

Are you using paramiko as a client or server?

Client

What feature(s) aren't working right?

Keys/auth

What version(s) of paramiko are you using?

3.1.0

What version(s) of Python are you using?

3.9.18

What operating system and version are you using?

RHEL 8.5.0-20

If you're connecting as a client, which SSH server are you connecting to?

It's not a server but a collection of switches with the port open to SSH

If you're using paramiko as part of another tool, which tool/version?

No response

What are you trying to do with paramiko?

I am trying to SSH Into our fleet of Juniper Devices and execute a command. This code previously worked in Python 2.7.5 with Paramiko 2.4.0, once i moved to the new versions of python and paramiko, it says the authentication has failed. Same script, correct path, correct user, correct host keys.

How are you trying to do it, and what's happening instead?

Keeps saying Authentcation has failed even though the script has not changed between going to version 2.7.5 to new pythons. I have rewrote said script to take into account python 3.9 but still nothing

Anything else?

#!/usr/bin/python3
import paramiko
import logging

Configure Paramiko logging to a file

paramiko_log_file = "paramiko_errors.log"
paramiko_log_level = logging.ERROR # Adjust the logging level as needed
paramiko_logger = logging.getLogger("paramiko")
paramiko_logger.setLevel(paramiko_log_level)
paramiko_file_handler = logging.FileHandler(paramiko_log_file)
paramiko_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
paramiko_file_handler.setFormatter(paramiko_formatter)
paramiko_logger.addHandler(paramiko_file_handler)

user = "user"
private_key_path = "/path/to/ssh/private/key"
commands = ["show version", "show chassis mac-address", "show virtual-chassis"]

Read the list of hosts from the text file

with open('hosts.txt', 'r') as f:
hosts = f.read().splitlines()

Create an SSH client instance

session = paramiko.SSHClient()

Automatically add host keys and ignore missing host keys

session.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
# Load the private key for authentication
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)

for host in hosts:
    print("Connecting to {}...".format(host))
    try:
        # Connect to the host using SSH key authentication
        session.connect(hostname=host, username=user, pkey=private_key)

        for command in commands:
            print("Executing command '{}' on {}".format(command, host))
            stdin, stdout, stderr = session.exec_command(command)

            # Read the command output
            output = stdout.read().decode().strip()
            error = stderr.read().decode().strip()

            # Check for errors
            if error:
                print("Error on {}: {}".format(host, error))
            else:
                print("Output from {}: {}".format(host, output))

    except paramiko.AuthenticationException as e:
        print("Authentication failed for {}: {}".format(host, e)) 
except paramiko.SSHException as e:
        print("SSH connection failed for {}: {}".format(host, e))
    except Exception as e:
        print("An error occurred for {}: {}".format(host, e))
    finally:
        # Close the SSH session for each host
        session.close()

except FileNotFoundError:
print("File 'hosts.txt' not found.")
finally:
# Close the SSH session outside of the loop
session.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant