How to resolve "[Errno 11001] getaddrinfo failed" when using Paramiko to change Cisco passwords?

49 views Asked by At

How to resolve "[Errno 11001] getaddrinfo failed" when using Paramiko to change Cisco passwords?

Question:

I'm working on a Python script to change passwords on Cisco devices remotely using Paramiko. However, I'm encountering an error "[Errno 11001] getaddrinfo failed" when trying to execute the script.

Here's a simplified version of the code:

`import paramiko
import openpyxl
import time

def read_credentials_from_excel( filename= "credentials.xlsx"):
    credentials = []
    try:
        workbook = openpyxl.load_workbook(filename= "credentials.xlsx")
        sheet = workbook.active
        for row in sheet.iter_rows(values_only=True):
            username, old_password, new_password, ip = row
            credentials.append((username, old_password, new_password, ip))
    except Exception as e:
        print("Error reading Excel file:", e)
    return credentials

def change_cisco_password(ip, username, old_password, new_password):
    # Create a SSH client
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        # Connect to the device
        client.connect(ip, username=username, password=old_password, timeout=5)

        # Create a shell
        shell = client.invoke_shell()


        # Send commands to change password
        shell.send("enable\n")
        shell.send(old_password + "\n")
        shell.send("configure terminal\n")
        shell.send(f"username {username} privilege 15 secret {new_password}\n")


        # Check if password change is successful
        output = shell.recv(65535).decode("utf-8")
        if f"Password updated successfully for user {username}" in output:
            print(f"Password updated successfully for {username} on {ip}!")
            return True
        else:
            print(f"Failed to update password for {username} on {ip}.")
            return False

    except Exception as e:
        print(f"Error: {e} for {username} on {ip}")
        return False
    finally:
        # Close the connection
        client.close()

def write_results_to_excel(results, output_file):
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    for result in results:
        username, ip, success = result
        sheet.append([username, ip, "Success" if success else "Failed"])
    workbook.save(output_file)

# Usage example
if __name__ == "__main__":
    input_excel_file = "input_credentials.xlsx"
    output_excel_file = "output_results.xlsx"

    credentials = read_credentials_from_excel(input_excel_file)
    results = []

    for username, old_password, new_password, ip in credentials:
        new_password = "new_password"  # Set the new password here
        success = change_cisco_password(ip, username, old_password, new_password)
        results.append((username, ip, success))

    write_results_to_excel(results, output_excel_file)`

When I run the script, I get the following errors:

"[Errno 11001] getaddrinfo failed for ip on new_password" "[Errno 11001] getaddrinfo failed for 192.168.4.10 on mehdi123" What could be causing these errors, and how can I resolve them?

Thank you for your help!

I expected the script to successfully connect to each Cisco device listed in the Excel file "input_credentials.xlsx" and change the password for the corresponding user. However, instead, I encountered the mentioned errors.

What could be causing these errors, and how can I modify the script to resolve them?

Thank you for your assistance!

0

There are 0 answers