DEV Community

Daniel Pepuho
Daniel Pepuho

Posted on • Edited on

12 2

How to solve 'authentication failed' in Paramiko

I got an error when running automation script for network device like this:

~python-Automation/ $ python3 multivendor.py 

Traceback (most recent call last):
  File "multivendor.py", line 23, in <module>
paramiko.ssh_exception.AuthenticationException: Authentication failed.

Enter fullscreen mode Exit fullscreen mode

so when you have entered the correct password and username but still get an error like that, you need to add this parameter in your automation script

look_for_keys=False
Enter fullscreen mode Exit fullscreen mode

finally, the parameter will disable search for discoverable private key files in ~/.ssh/.

so you will see my script like this

import paramiko
import time

devices = [
    {
        'ip_address' : '172.16.1.1',
        'vendor' : 'mikrotik',
        'username' : 'router1',
        'password' : 'tes'
    },
    {
        'ip_address' : '172.16.1.2',
        'vendor' : 'cisco',
        'username' : 'cisco',
        'password' : 'cisco'
    }
]

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for device in devices:
    ssh_client.connect(hostname=device['ip_address'],
                                username=device['username'],
                                password=device['password'],
                                look_for_keys=False)

    print("success login to {}".format(device['ip_address']))

    if device['vendor'] == 'cisco':
        conn = ssh_client.invoke_shell()

        # conn.send("enable\n")
        conn.send("conf t\n")
        conn.send("int lo99\n")
        conn.send("ip add 10.1.99.1 255.255.255.255\n")
        time.sleep(2)

        # output = conn.recv(65535)
        # print(output.decode())
    else:
        ssh_client.exec_command('interface bridge add name=loopback3\n')
        ssh_client.exec_command('ip add add address=10.2.99.2/32 interface=loopback3')

ssh_client.close

Enter fullscreen mode Exit fullscreen mode

Good luck 😄

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more