07-24-2022 12:19 PM
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)
07-25-2022 06:25 AM
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
07-25-2022 11:43 PM - edited 07-25-2022 11:44 PM
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)
07-26-2022 12:16 AM
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.
07-26-2022 01:08 AM - edited 07-26-2022 01:20 AM
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?
07-26-2022 02:22 AM
["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..
07-26-2022 12:26 AM
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:
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide