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 Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay