Python Forum
Problem connecting to 2 switches, HELP!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem connecting to 2 switches, HELP!
#1
I have configured 2 Cisco switches with ssh and have given interface vlan1 on s1 the ip address of 192.168.2.111 and s2 192.168.2.112. I can ssh into them fine using CLI

I am trying to create a simple script using netmiko to ssh into both switches and apply Cisco configuration commands to configure trunk ports, access ports and vlans. I am using a device list to specify the ip address for the script to connect to. With the first line of the device list as s1 192.168.2.111 and the second line of the list is s2 192.168.2.112. The problem I am having is that the script is only seems to be applying the configuration to the last ip address in the devices list as in s2 192.168.2.112. You will see from the output of the code that the configuration has only been applied to s2 where it should be applied to both.

Can anyone help me out in how to chance my code so that both switches are configured not just the last one in the device list. Would be much appreciated!

Device list looks something like this:

~~~###device_ip_list###~~~
192.168.2.111
192.168.2.112

Here is a sample of my code:

from netmiko import ConnectHandler



with open('device_ip_list') as f:
    device_ip_list = f.read().splitlines()


for devices in device_ip_list:
    print ('Connecting to device ' + devices)
    ip_address_of_device = devices
    net_device = {
        'device_type': 'cisco_ios',
        'ip': ip_address_of_device,
        'username': 'osboxes',
        'password': 'cisco12345'
    }

    net_connect = ConnectHandler(**net_device)
    

    
for devices in device_ip_list:
    net_connect = ConnectHandler(**net_device)
    for i in range (1):
         print ('Creating VLAN 30')
         vlan30_commands = ('vlan ' + str(30), 'name Student',) 
    output = net_connect.send_config_set(vlan30_commands)
print(output)


for devices in device_ip_list:
    net_connect = ConnectHandler(**net_device)
    for i in range (1):
         print ('Creating VLAN 40')
         vlan40_commands = ('vlan ' + str(40), 'name Staff',)
         output = net_connect.send_config_set(vlan40_commands)
         print(output)

for devices in device_ip_list:
    net_connect = ConnectHandler(**net_device)
    for i in range (1):
         print ('Creating VLAN 99')
         vlan99_commands = ('vlan ' + str(99), 'name Managment',)
         output = net_connect.send_config_set(vlan99_commands)
         print(output)


for devices in device_ip_list:
    net_connect = ConnectHandler(**net_device)
    for i in range (1):
         print ('Creating Trunk Ports')
         trunk_commands = ('int range f0/16 , f0/24', 'switchport mode trunk')
         output = net_connect.send_config_set(trunk_commands)
         print(output)

for devices in device_ip_list:
    net_connect = ConnectHandler(**net_device)
    for i in range (1):
         print ('Creating Access Ports')
         access_commands = ('int f0/1', 'switchport mode access', 'switchport access vlan 30', 'int f0/2', 'switchport mode access', 'switchport access vlan 40', 'int f0/3', 'switchport mode access', 'switchport access vlan 99')
         output = net_connect.send_config_set(access_commands)
         print(output)
This is the output I get, you will see that only s2 has been configured and looks something like this:
Output:

Connecting to device 192.168.2.111
Connecting to device 192.168.2.112
Creating VLAN 30
Creating VLAN 30
config term
Enter configuration commands, one per line. End with CNTL/Z.
S2(config)#vlan 30
S2(config-vlan)#name Student
S2(config-vlan)#end
S2#
Creating VLAN 40
config term
Enter configuration commands, one per line. End with CNTL/Z.
S2(config)#vlan 40
S2(config-vlan)#name Staff
S2(config-vlan)#end
S2#
Creating VLAN 40
config term
Enter configuration commands, one per line. End with CNTL/Z.
S2(config)#vlan 40
S2(config-vlan)#name Staff
S2(config-vlan)#end
S2#
Creating VLAN 99
config term
Enter configuration commands, one per line. End with CNTL/Z.
S2(config)#vlan 99
S2(config-vlan)#name Managment
S2(config-vlan)#end
S2#
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert looping home security to a one time scan of switches and sensors duckredbeard 0 1,749 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  Problem connecting Zimbra with Python 3.7 kobakajoao 0 2,138 Sep-17-2018, 08:41 PM
Last Post: kobakajoao
  Relay switches On and Off every cylcle... hotwalk 6 3,438 Nov-29-2017, 05:50 PM
Last Post: hotwalk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020