├── Cisco-Wired-Campus-Design ├── README.md ├── S1script2.py ├── S1script3.py ├── S1script3inband.py ├── install python ├── iosv_l2_cisco_design ├── netmiko1.py ├── netmiko2.py ├── netmiko3.py ├── netmiko4.py ├── netmiko5.py ├── netmiko6.py ├── paramiko-pythonssh1.py ├── pythonR1script1.py ├── pythonS1script1.py ├── saveconfigs.py └── switchloopfile.py /Cisco-Wired-Campus-Design: -------------------------------------------------------------------------------- 1 | vtp mode transparent 2 | spanning-tree mode rapid-pvst 3 | udld enable 4 | errdisable recovery cause all 5 | port-channel load-balance src-dst-ip 6 | ip name-server 8.8.8.8 7 | no ip http server 8 | ip http secure-server 9 | 10 | snmp-server community python1 RO 11 | snmp-server community python2 RW 12 | 13 | ntp server 87.81.181.2 14 | ntp update-calendar 15 | 16 | clock timezone PST -8 17 | clock summer-time PDT recurring 18 | service timestamps debug datetime msec localtime 19 | service timestamps log datetime msec localtime 20 | 21 | vlan 100 22 | name Data 23 | vlan 101 24 | name Voice 25 | 26 | interface vlan 1 27 | description In-band Management 28 | 29 | ip default-gateway 192.168.122.1 30 | ip dhcp snooping vlan 100,101 31 | 32 | no ip dhcp snooping information option 33 | ip dhcp snooping 34 | ip arp inspection vlan 100,101 35 | spanning-tree portfast bpduguard default 36 | ipv6 nd raguard policy HOST_POLICY 37 | device-role host 38 | 39 | interface range GigabitEthernet 2/0 - 3 40 | switchport access vlan 100 41 | switchport voice vlan 101 42 | switchport host 43 | switchport port-security maximum 2 44 | switchport port-security 45 | switchport port-security aging time 2 46 | switchport port-security aging type inactivity 47 | switchport port-security violation restrict 48 | ip arp inspection limit rate 100 49 | ip dhcp snooping limit rate 100 50 | ip verify source 51 | ipv6 nd raguard attach-policy HOST_POLICY -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pythonvideos 2 | Code examples for David Bombal's Python For Network Engineers YouTube Videos 3 | 4 | You can find the GNS3 Python YouTube videos here: 5 | https://www.youtube.com/playlist?list=PLhfrWIlLOoKPn7T9FtvbOWX8GxgsFFNwn 6 | -------------------------------------------------------------------------------- /S1script2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import sys 5 | import telnetlib 6 | 7 | HOST = "192.168.122.72" 8 | user = raw_input("Enter your telnet username: ") 9 | password = getpass.getpass() 10 | 11 | tn = telnetlib.Telnet(HOST) 12 | 13 | tn.read_until("Username: ") 14 | tn.write(user + "\n") 15 | if password: 16 | tn.read_until("Password: ") 17 | tn.write(password + "\n") 18 | 19 | tn.write("conf t\n") 20 | 21 | for n in range (2,101): 22 | tn.write("vlan " + str(n) + "\n") 23 | tn.write("name Python_VLAN_" + str(n) + "\n") 24 | 25 | tn.write("end\n") 26 | tn.write("exit\n") 27 | 28 | print tn.read_all() 29 | -------------------------------------------------------------------------------- /S1script3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import sys 5 | import telnetlib 6 | 7 | user = raw_input("Enter your telnet username: ") 8 | password = getpass.getpass() 9 | 10 | 11 | for n in range (72,77): 12 | print "Telnet to host" + str(n) 13 | HOST = "192.168.122." + str(n) 14 | tn = telnetlib.Telnet(HOST) 15 | 16 | tn.read_until("Username: ") 17 | tn.write(user + "\n") 18 | if password: 19 | tn.read_until("Password: ") 20 | tn.write(password + "\n") 21 | 22 | tn.write("conf t\n") 23 | 24 | for n in range (2,21): 25 | tn.write("vlan " + str(n) + "\n") 26 | tn.write("name Python_VLAN_" + str(n) + "\n") 27 | 28 | tn.write("end\n") 29 | tn.write("exit\n") 30 | 31 | print tn.read_all() 32 | 33 | -------------------------------------------------------------------------------- /S1script3inband.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import sys 5 | import telnetlib 6 | 7 | user = raw_input("Enter your telnet username: ") 8 | password = getpass.getpass() 9 | 10 | 11 | for n in range (72,77): 12 | print "Telnet to host" + str(n) 13 | HOST = "192.168.122." + str(n) 14 | tn = telnetlib.Telnet(HOST) 15 | 16 | tn.read_until("Username: ") 17 | tn.write(user + "\n") 18 | 19 | if password: 20 | tn.read_until("Password: ") 21 | tn.write(password + "\n") 22 | 23 | tn.write("conf t\n") 24 | 25 | for n in range (2,21): 26 | tn.write("vlan " + str(n) + "\n") 27 | tn.write("name Python_VLAN_" + str(n) + "\n") 28 | 29 | tn.write("end\n") 30 | tn.write("wr\n") 31 | tn.write("exit\n") 32 | print tn.read_all() 33 | -------------------------------------------------------------------------------- /install python: -------------------------------------------------------------------------------- 1 | apt-get update 2 | apt-get install python -y 3 | apt-get install build-essential libssl-dev libffi-dev -y 4 | apt-get install python-pip -y 5 | pip install cryptography 6 | pip install --upgrade pip 7 | pip install paramiko 8 | pip install netmiko 9 | -------------------------------------------------------------------------------- /iosv_l2_cisco_design: -------------------------------------------------------------------------------- 1 | vtp mode transparent 2 | spanning-tree mode rapid-pvst 3 | udld enable 4 | errdisable recovery cause all 5 | port-channel load-balance src-dst-ip 6 | ip name-server 8.8.8.8 7 | no ip http server 8 | ip http secure-server 9 | 10 | snmp-server community python1 RO 11 | snmp-server community python2 RW 12 | 13 | ntp server 87.81.181.2 14 | ntp update-calendar 15 | 16 | clock timezone PST -8 17 | clock summer-time PDT recurring 18 | service timestamps debug datetime msec localtime 19 | service timestamps log datetime msec localtime 20 | 21 | vlan 100 22 | name Data 23 | vlan 101 24 | name Voice 25 | 26 | interface vlan 1 27 | description In-band Management 28 | 29 | ip default-gateway 192.168.122.1 30 | ip dhcp snooping vlan 100,101 31 | 32 | no ip dhcp snooping information option 33 | ip dhcp snooping 34 | ip arp inspection vlan 100,101 35 | spanning-tree portfast bpduguard default 36 | ipv6 nd raguard policy HOST_POLICY 37 | device-role host 38 | 39 | interface range GigabitEthernet 2/0 - 3 40 | switchport access vlan 100 41 | switchport voice vlan 101 42 | switchport host 43 | switchport port-security maximum 2 44 | switchport port-security 45 | switchport port-security aging time 2 46 | switchport port-security aging type inactivity 47 | switchport port-security violation restrict 48 | ip arp inspection limit rate 100 49 | ip dhcp snooping limit rate 100 50 | ip verify source 51 | ipv6 nd raguard attach-policy HOST_POLICY -------------------------------------------------------------------------------- /netmiko1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.72', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | 13 | net_connect = ConnectHandler(**iosv_l2) 14 | #net_connect.find_prompt() 15 | output = net_connect.send_command('show ip int brief') 16 | print output 17 | 18 | config_commands = ['int loop 0', 'ip address 1.1.1.1 255.255.255.0'] 19 | output = net_connect.send_config_set(config_commands) 20 | print output 21 | 22 | for n in range (2,21): 23 | print "Creating VLAN " + str(n) 24 | config_commands = ['vlan ' + str(n), 'name Python_VLAN ' + str(n)] 25 | output = net_connect.send_config_set(config_commands) 26 | print output 27 | -------------------------------------------------------------------------------- /netmiko2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.71', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.122.72', 15 | 'username': 'david', 16 | 'password': 'cisco', 17 | } 18 | 19 | iosv_l2_s3 = { 20 | 'device_type': 'cisco_ios', 21 | 'ip': '192.168.122.73', 22 | 'username': 'david', 23 | 'password': 'cisco', 24 | } 25 | 26 | 27 | all_devices = [iosv_l2_s1, iosv_l2_s2, iosv_l2_s3] 28 | 29 | for devices in all_devices: 30 | net_connect = ConnectHandler(**devices) 31 | for n in range (2,21): 32 | print "Creating VLAN " + str(n) 33 | config_commands = ['vlan ' + str(n), 'name Python_VLAN ' + str(n)] 34 | output = net_connect.send_config_set(config_commands) 35 | print output 36 | -------------------------------------------------------------------------------- /netmiko3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.71', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.122.72', 15 | 'username': 'david', 16 | 'password': 'cisco', 17 | } 18 | 19 | iosv_l2_s3 = { 20 | 'device_type': 'cisco_ios', 21 | 'ip': '192.168.122.73', 22 | 'username': 'david', 23 | 'password': 'cisco', 24 | } 25 | 26 | with open('iosv_l2_config1') as f: 27 | lines = f.read().splitlines() 28 | print lines 29 | 30 | all_devices = [iosv_l2_s1, iosv_l2_s2, iosv_l2_s3] 31 | 32 | for devices in all_devices: 33 | net_connect = ConnectHandler(**devices) 34 | output = net_connect.send_config_set(lines) 35 | print output 36 | -------------------------------------------------------------------------------- /netmiko4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.71', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.122.72', 15 | 'username': 'david', 16 | 'password': 'cisco', 17 | } 18 | 19 | iosv_l2_s3 = { 20 | 'device_type': 'cisco_ios', 21 | 'ip': '192.168.122.73', 22 | 'username': 'david', 23 | 'password': 'cisco', 24 | } 25 | 26 | iosv_l2_s4 = { 27 | 'device_type': 'cisco_ios', 28 | 'ip': '192.168.122.74', 29 | 'username': 'david', 30 | 'password': 'cisco', 31 | } 32 | 33 | iosv_l2_s5 = { 34 | 'device_type': 'cisco_ios', 35 | 'ip': '192.168.122.75', 36 | 'username': 'david', 37 | 'password': 'cisco', 38 | } 39 | 40 | 41 | with open('iosv_l2_access') as f: 42 | lines = f.read().splitlines() 43 | print lines 44 | 45 | 46 | all_devices = [iosv_l2_s5, iosv_l2_s4, iosv_l2_s3] 47 | 48 | for devices in all_devices: 49 | net_connect = ConnectHandler(**devices) 50 | output = net_connect.send_config_set(lines) 51 | print output 52 | 53 | 54 | with open('iosv_l2_core') as f: 55 | lines = f.read().splitlines() 56 | print lines 57 | 58 | 59 | all_devices = [iosv_l2_s2, iosv_l2_s1] 60 | 61 | for devices in all_devices: 62 | net_connect = ConnectHandler(**devices) 63 | output = net_connect.send_config_set(lines) 64 | print output 65 | 66 | 67 | -------------------------------------------------------------------------------- /netmiko5.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.71', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.122.72', 15 | 'username': 'david', 16 | 'password': 'cisco', 17 | } 18 | 19 | iosv_l2_s3 = { 20 | 'device_type': 'cisco_ios', 21 | 'ip': '192.168.122.73', 22 | 'username': 'david', 23 | 'password': 'cisco', 24 | } 25 | 26 | iosv_l2_s4 = { 27 | 'device_type': 'cisco_ios', 28 | 'ip': '192.168.122.74', 29 | 'username': 'david', 30 | 'password': 'cisco', 31 | } 32 | 33 | iosv_l2_s5 = { 34 | 'device_type': 'cisco_ios', 35 | 'ip': '192.168.122.75', 36 | 'username': 'david', 37 | 'password': 'cisco', 38 | } 39 | 40 | 41 | with open('iosv_l2_cisco_design') as f: 42 | lines = f.read().splitlines() 43 | print lines 44 | 45 | 46 | all_devices = [iosv_l2_s5, iosv_l2_s4, iosv_l2_s3] 47 | 48 | for devices in all_devices: 49 | net_connect = ConnectHandler(**devices) 50 | output = net_connect.send_config_set(lines) 51 | print output 52 | 53 | 54 | with open('iosv_l2_core') as f: 55 | lines = f.read().splitlines() 56 | print lines 57 | 58 | 59 | all_devices = [iosv_l2_s2, iosv_l2_s1] 60 | 61 | for devices in all_devices: 62 | net_connect = ConnectHandler(**devices) 63 | output = net_connect.send_config_set(lines) 64 | print output 65 | -------------------------------------------------------------------------------- /netmiko6.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from netmiko import ConnectHandler 4 | 5 | iosv_l2_s1 = { 6 | 'device_type': 'cisco_ios', 7 | 'ip': '192.168.122.71', 8 | 'username': 'david', 9 | 'password': 'cisco', 10 | } 11 | 12 | iosv_l2_s2 = { 13 | 'device_type': 'cisco_ios', 14 | 'ip': '192.168.122.72', 15 | 'username': 'david', 16 | 'password': 'cisco', 17 | } 18 | 19 | iosv_l2_s3 = { 20 | 'device_type': 'cisco_ios', 21 | 'ip': '192.168.122.73', 22 | 'username': 'david', 23 | 'password': 'cisco', 24 | } 25 | 26 | iosv_l2_s4 = { 27 | 'device_type': 'cisco_ios', 28 | 'ip': '192.168.122.74', 29 | 'username': 'david', 30 | 'password': 'cisco', 31 | } 32 | 33 | iosv_l2_s5 = { 34 | 'device_type': 'cisco_ios', 35 | 'ip': '192.168.122.75', 36 | 'username': 'david', 37 | 'password': 'cisco', 38 | } 39 | 40 | 41 | with open('iosv_l2_cisco_design') as f: 42 | lines = f.read().splitlines() 43 | print lines 44 | 45 | 46 | all_devices = [iosv_l2_s5, iosv_l2_s4, iosv_l2_s3] 47 | 48 | for devices in all_devices: 49 | net_connect = ConnectHandler(**devices) 50 | output = net_connect.send_config_set(lines) 51 | print output 52 | 53 | 54 | with open('iosv_l2_core') as f: 55 | lines = f.read().splitlines() 56 | print lines 57 | 58 | 59 | all_devices = [iosv_l2_s2, iosv_l2_s1] 60 | 61 | for devices in all_devices: 62 | net_connect = ConnectHandler(**devices) 63 | output = net_connect.send_config_set(lines) 64 | print output 65 | -------------------------------------------------------------------------------- /paramiko-pythonssh1.py: -------------------------------------------------------------------------------- 1 | import paramiko 2 | import time 3 | 4 | ip_address = "192.168.122.72" 5 | username = "david" 6 | password = "cisco" 7 | 8 | ssh_client = paramiko.SSHClient() 9 | ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 10 | ssh_client.connect(hostname=ip_address,username=username,password=password) 11 | 12 | print "Successful connection", ip_address 13 | 14 | remote_connection = ssh_client.invoke_shell() 15 | 16 | remote_connection.send("configure terminal\n") 17 | remote_connection.send("int loop 0\n") 18 | remote_connection.send("ip address 1.1.1.1 255.255.255.255\n") 19 | remote_connection.send("int loop 1\n") 20 | remote_connection.send("ip address 2.2.2.2 255.255.255.255\n") 21 | remote_connection.send("router ospf 1\n") 22 | remote_connection.send("network 0.0.0.0 255.255.255.255 area 0\n") 23 | 24 | for n in range (2,21): 25 | print "Creating VLAN " + str(n) 26 | remote_connection.send("vlan " + str(n) + "\n") 27 | remote_connection.send("name Python_VLAN " + str(n) + "\n") 28 | time.sleep(0.5) 29 | 30 | remote_connection.send("end\n") 31 | 32 | time.sleep(1) 33 | output = remote_connection.recv(65535) 34 | print output 35 | 36 | ssh_client.close -------------------------------------------------------------------------------- /pythonR1script1.py: -------------------------------------------------------------------------------- 1 | import getpass 2 | import sys 3 | import telnetlib 4 | 5 | HOST = "192.168.122.71" 6 | user = raw_input("Enter your telnet username: ") 7 | password = getpass.getpass() 8 | 9 | tn = telnetlib.Telnet(HOST) 10 | 11 | tn.read_until("Username: ") 12 | tn.write(user + "\n") 13 | if password: 14 | tn.read_until("Password: ") 15 | tn.write(password + "\n") 16 | 17 | tn.write("enable\n") 18 | tn.write("cisco\n") 19 | tn.write("conf t\n") 20 | tn.write("int loop 0\n") 21 | tn.write("ip address 1.1.1.1 255.255.255.255\n") 22 | tn.write("int loop 1\n") 23 | tn.write("ip address 2.2.2.2 255.255.255.255\n") 24 | tn.write("router ospf 1\n") 25 | tn.write("network 0.0.0.0 255.255.255.255 area 0\n") 26 | tn.write("end\n") 27 | tn.write("exit\n") 28 | 29 | print tn.read_all() 30 | -------------------------------------------------------------------------------- /pythonS1script1.py: -------------------------------------------------------------------------------- 1 | import getpass 2 | import sys 3 | import telnetlib 4 | 5 | HOST = "192.168.122.72" 6 | user = raw_input("Enter your telnet username: ") 7 | password = getpass.getpass() 8 | 9 | tn = telnetlib.Telnet(HOST) 10 | 11 | tn.read_until("Username: ") 12 | tn.write(user + "\n") 13 | if password: 14 | tn.read_until("Password: ") 15 | tn.write(password + "\n") 16 | 17 | tn.write("enable\n") 18 | tn.write("cisco\n") 19 | tn.write("conf t\n") 20 | tn.write("vlan 2\n") 21 | tn.write("name Python_VLAN_2\n") 22 | tn.write("exit\n") 23 | tn.write("vlan 3\n") 24 | tn.write("name Python_VLAN_3\n") 25 | tn.write("exit\n") 26 | tn.write("vlan 4\n") 27 | tn.write("name Python_VLAN_4\n") 28 | tn.write("exit\n") 29 | tn.write("vlan 5\n") 30 | tn.write("name Python_VLAN_5\n") 31 | tn.write("exit\n") 32 | tn.write("vlan 6\n") 33 | tn.write("name Python_VLAN_6\n") 34 | tn.write("exit\n") 35 | 36 | tn.write("end\n") 37 | tn.write("exit\n") 38 | 39 | print tn.read_all() -------------------------------------------------------------------------------- /saveconfigs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | #import sys 5 | import telnetlib 6 | 7 | # Get Username and Password 8 | user = raw_input("Enter your username: ") 9 | password = getpass.getpass() 10 | 11 | # Open file with list of switches 12 | f = open ("myswitches") 13 | 14 | # Telnet to each switch and cofigure it 15 | for line in f: 16 | print "Getting running-config " + (line) 17 | HOST = line.strip() 18 | tn = telnetlib.Telnet(HOST) 19 | 20 | tn.read_until("Username: ") 21 | tn.write(user + "\n") 22 | if password: 23 | tn.read_until("Password: ") 24 | tn.write(password + "\n") 25 | 26 | tn.write("terminal length 0\n") 27 | tn.write("show run\n") 28 | tn.write("exit\n") 29 | 30 | readoutput = tn.read_all() 31 | saveoutput = open("switch" + HOST, "w") 32 | saveoutput.write(readoutput) 33 | saveoutput.write("\n") 34 | saveoutput.close 35 | print tn.read_all() 36 | -------------------------------------------------------------------------------- /switchloopfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import getpass 4 | import sys 5 | import telnetlib 6 | 7 | user = raw_input("Enter your username: ") 8 | password = getpass.getpass() 9 | 10 | f = open ('myswitches') 11 | 12 | for line in f: 13 | print "Configuring Switch " + (line) 14 | HOST = line 15 | tn = telnetlib.Telnet(HOST) 16 | 17 | tn.read_until("Username: ") 18 | tn.write(user + "\n") 19 | if password: 20 | tn.read_until("Password: ") 21 | tn.write(password + "\n") 22 | 23 | tn.write("conf t\n") 24 | 25 | for n in range (2,26): 26 | tn.write("vlan " + str(n) + "\n") 27 | tn.write("name Python_VLAN_" + str(n) + "\n") 28 | 29 | tn.write("end\n") 30 | tn.write("exit\n") 31 | 32 | print tn.read_all() 33 | 34 | 35 | --------------------------------------------------------------------------------