You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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()
The text was updated successfully, but these errors were encountered:
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)
except FileNotFoundError:
print("File 'hosts.txt' not found.")
finally:
# Close the SSH session outside of the loop
session.close()
The text was updated successfully, but these errors were encountered: