cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1144
Views
0
Helpful
6
Replies

Netmiko on many devices

Moudar
VIP
VIP

What i need is how to apply this code on many devices?
I tried to make a text file with many IP addresses in it, but the code was applied only on the last IP i had on my txt list!

 

from netmiko import ConnectHandler
from getpass import getpass

password = getpass()

net_connect = ConnectHandler(host="172.22.1.97", username="cisco", password=password, device_type="cisco_ios", secret='cisco')

show_vlan_command = net_connect.send_command('show vlan b', use_textfsm=True)
net_connect.enable() #enable mode

which_vlan = input("wich VLAN do you want to check?: ")
#print(which_vlan)

for i in show_vlan_command:
if i['vlan_id'] == which_vlan: 
print(quit('VLAN is already there')) #to break out the code if a vlan already there!

for x in show_vlan_command:
if x['vlan_id'] != which_vlan:
print(f'VLAN is not there, create VLAN')
break

vlan_name = input("vlan name?: ")

create_new_vlan = [
(f'vlan {which_vlan}'),
(f'name {vlan_name}'),
]
create_vlan = net_connect.send_config_set(create_new_vlan)
#print(create_vlan)
show_vlan_command = net_connect.send_command(f'show vlan b | include {vilket_vlan}', use_textfsm=True)
print(show_vlan_command)

6 Replies 6

Jon Marshall
Hall of Fame
Hall of Fame

 

I can't see any code here that would read in the text file and loop through the IPs ? 

 

Also when you post your code there is an option to insert as code which makes it much more readable. 

 

Jon

Moudar
VIP
VIP

 

from netmiko import ConnectHandler
from getpass import getpass

password = getpass()

net_connect = ConnectHandler(host="172.22.1.97", username="cisco", password=password, device_type="cisco_ios", secret='cisco')

show_vlan_command = net_connect.send_command('show vlan b', use_textfsm=True)
net_connect.enable() #enable mode

which_vlan = input("wich VLAN do you want to check?: ")
#print(which_vlan)

for i in show_vlan_command:
if i['vlan_id'] == which_vlan: 
print(quit('VLAN is already there')) #to break out the code if a vlan already there!

for x in show_vlan_command:
if x['vlan_id'] != which_vlan:
print(f'VLAN is not there, create VLAN')
break

vlan_name = input("vlan name?: ")

create_new_vlan = [
(f'vlan {which_vlan}'),
(f'name {vlan_name}'),
]
create_vlan = net_connect.send_config_set(create_new_vlan)
#print(create_vlan)
show_vlan_command = net_connect.send_command(f'show vlan b | include {vilket_vlan}', use_textfsm=True)
print(show_vlan_command)

 

carstenlymann1
Level 1
Level 1

Hi @Moudar 

I agree with @Jon Marshall  there is no point in your code where you loop through a list of ip adresse.

Here is what i have done in my code :

import json
import os

working_dir = os.getcwd()

try:
    with open(working_dir + '/device_list.txt', 'r') as json_file:
        list_of_ip = json.load(json_file)
        for l in list_of_ip:
            print(l)
except:
    print('No ip list found')

for ip in list_of_ip:

    device = {
        'device_type': 'cisco_ios',
        'ip': ip,
        'username': username,
        'password': password
    }

 I use a try where i print the ip out, so i can see that ip while script is running.

And then the for loop, to loop through the list_of_ip, to use the ip variable to put into the device variable. 

And then everything you wants to do, you put that under the for loop as well. (what i usely still do is start out with only having some basic show commands in my script. That way if i have made an error, i want harm my device if its done in production devices.)

["192.168.1.10", "192.168.1.20", "192.168.1.30", "192.168.1.40", "192.168.1.50", "192.168.1.60"]

And this could be how the device_list.txt is made.

Feel free to ask if you got any questions. 

 

 

Please remember to select a correct answer and rate helpful posts
/ Carsten

My devices.txt file looks like this:

 

172.22.2.1
172.22.2.2
172.22.2.3

 

is this right?

how should a json file with ip addresses look like?

["192.168.1.10", "192.168.1.20", "192.168.1.30", "192.168.1.40", "192.168.1.50", "192.168.1.60"]

This is how my txt file is..

 

Please remember to select a correct answer and rate helpful posts
/ Carsten

Hello,

I agree with @carstenlymann1 and @Jon Marshall , there is no reference to any txt file in your code. Also, in your code, since you apparently have configured 'enable secret' passwords, you need to reference that password:

net_connect = ConnectHandler(host="172.22.1.97", username="cisco", password=password, secret=xxx, device_type="cisco_ios", secret='cisco')

The link below has I think a pretty good sample config:

https://underdogit.net/2021/08/10/use-netmiko-to-connect-to-multiple-devices-with-a-text-file-100daysofcode/

Review Cisco Networking for a $25 gift card