0

How to connect multiple cisco devices via ssh connecthandle.. will this work. I'm new automation, Please help Host=open('devcies.txt','r') devices=Host.readlines() for ip in devices : local_login(ip) print (ip) Host.close()

def local_login('ipaddr'):

     devlist = {'device_type': 'cisco_ios',
       'ip': ipaddr,
       'username': klsdjfglkfj,
       'password': yyyyyyyy,
        'secret': xxxxxxx,
     }
  net_connect = ConnectHandler(**devlist)
alwynram
  • 1
  • 1

2 Answers2

0

It's very simple you can do it by using python's basic logic.

Create one function to login to the device and task as you want and call that function multiple times.

So, let's say you have one list of multiple devices.

device_list = ["10.1.3.4","10.10.4.5","10.10.5.6"]

Function to Login into the device :

def login_device(ipaddr):

    devlist = {'device_type': 'cisco_ios',
       'ip': ipaddr,
       'username': klsdjfglkfj,
       'password': yyyyyyyy,
        'secret': xxxxxxx,
     }

    net_connect = ConnectHandler(**devlist)
    print("connection done")

Apply for loop to iterate over the device list and call the function.

device_list = ["10.1.3.4","10.10.4.5",10.10.5.6"]
for ipaddr in device_list:
    login_device(ipaddr)

If you want to read it's from a text file then you can do it like this:

Suppose IPs are stored in "devices.txt".

with open('/path/routers.txt') as f:
    device_list = f.read().splitlines()
    for ipaddr in device_list:
        login_device(ipaddr)
Sachin
  • 1,460
  • 17
  • 24
0

With this code, you can run commands on many devices at the same time. You can also use it by hiding your identity information with user_pass. There is also a device prompt discovery feature.

with open("user_pass.txt", "r") as f5:
    user_pass = f5.readlines()

for list_user_pass in user_pass:
    if "username" in list_user_pass:
        username = list_user_pass.split(":")[1].strip()
    if "password" in list_user_pass:
        password = list_user_pass.split(":")[1].strip()

    def _ssh_(nodeip):
        try:
        access_mpls = {
            'device_type': 'huawei_olt', 'ip': nodeip, 'username':
                username, 'password': password, }
        net_connect = Netmiko(**access_mpls)
        print(nodeip.strip() + "  " + "success enter")
    except Exception as e:
        print(e)
        f_3.write(nodeip.strip() + "\n")
        return

    prompt_gpon_fnk = net_connect.find_prompt()
    hostname_fnk = prompt_gpon_fnk.strip("<" + ">")
    print(hostname_fnk)
    net_connect.send_command_timing("enable")
    net_connect.send_command_timing("undo smart")
    output = net_connect.send_command_timing("config")
    print("config moda girildi")
    net_connect.send_command_timing("acl 2010 ")
    net_connect.send_command_timing("quit")
    net_connect.send_command_timing("save")

    print("config done")
    with open("MDU_OK_2.txt", "a") as f:
        f.write(nodeip + "\n")
        f.close()

    net_connect.disconnect()

f_2 = open("ip_list_2.txt", "r")
ip_list = f_2.readlines()
f_2.close()
f_3 = open("ssh_unsuccess_2.txt", "w")

myPool = ThreadPool(100)
result = myPool.map(_ssh_, ip_list)
Mert Kulac
  • 294
  • 2
  • 19