#SCRIPT 1: import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn123 = telnetlib.Telnet(HOST) tn123.read_until(b"Username: ") tn123.write(user.encode("utf8") + b"\n") tn123.read_until(b"Password: ") tn123.write(password.encode("utf8") + b"\n") #telnet established tn123.write(b"show clock\n") print(tn123.read_all().decode("utf8"))
#SCRIPT 2: import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn123 = telnetlib.Telnet(HOST) tn123.read_until(b"Username: ") tn123.write(user.encode() + b"\n") tn123.read_until(b"Password: ") tn123.write(password.encode() + b"\n") #telnet established tn123.write(b"show clock\n") print(tn123.read_all().decode())
#SCRIPT 3: import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn123 = telnetlib.Telnet(HOST) tn123.read_until(b"Username: ") tn123.write(user.encode("utf-8") + b"\n") tn123.read_until(b"Password: ") tn123.write(password.encode("utf-8") + b"\n") #telnet established tn123.write(b"show clock\n") print(tn123.read_all().decode("utf-8"))
#SCRIPT 4: import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn123 = telnetlib.Telnet(HOST) tn123.read_until(b"Username: ") tn123.write(user.encode("ascii") + b"\n") tn123.read_until(b"Password: ") tn123.write(password.encode("ascii") + b"\n") #telnet established tn123.write(b"show clock\n") print(tn123.read_all().decode("ascii"))
#SCRIPT 5: import getpass import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn = telnetlib.Telnet(HOST) # to open telnet connection tn.read_until(b"Username: ") tn.write(user.encode('utf8') + b"\n") tn.read_until(b"Password: ") tn.write(password.encode('utf8') + b"\n") tn.write(b"enable\n") # \n represents end of line tn.write(b"cisco\n") tn.write(b"term len 0\n") tn.write(b"sh run\n") tn.write(b"conf t\n") tn.write(b"int loop 0\n") tn.write(b"ip address 1.1.1.1 255.255.255.255\n") tn.write(b"int loop 1\n") tn.write(b"ip address 2.2.2.2 255.255.255.255\n") tn.write(b"router ospf 1\n") tn.write(b"network 0.0.0.0 255.255.255.255 area 0\n") tn.write(b"end\n") tn.write(b"exit\n") # to close the connection print(tn.read_all().decode('utf8'))
#SCRIPT 6: import getpass import telnetlib HOST = "172.16.221.106" user = input("Enter your telnet username: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) # to open telnet connection tn.read_until(b"Username: ") tn.write(user.encode('utf8') + b"\n") tn.read_until(b"Password: ") tn.write(password.encode('utf8') + b"\n") tn.write(b"enable\n") # \n represents end of line tn.write(b"cisco\n") tn.write(b"term len 0\n") tn.write(b"sh run\n") tn.write(b"conf t\n") tn.write(b"int loop 0\n") tn.write(b"ip address 1.1.1.1 255.255.255.255\n") tn.write(b"int loop 1\n") tn.write(b"ip address 2.2.2.2 255.255.255.255\n") tn.write(b"router ospf 1\n") tn.write(b"network 0.0.0.0 255.255.255.255 area 0\n") tn.write(b"end\n") tn.write(b"exit\n") # to close the connection print(tn.read_all().decode('utf8'))
#SCRIPT 7: import getpass import telnetlib HOST = "192.168.32.200" user = "admin" password = "cisco" tn = telnetlib.Telnet(HOST) tn.read_until(b"Username: ") tn.write(user.encode("utf8") + b"\n") tn.read_until(b"Password:") tn.write(password.encode("utf8") + b"\n") tn.write(b"conf t\n") tn.write(b"hostname SWITCH-PYTHON\n") tn.write(b"exit\n") tn.write(b"terminal length 0\n") tn.write(b"show run\n") tn.write(b"exit\n") #Save method readoutput = tn.read_all().decode("utf8") saveoutput = open("backup_config_of_" + HOST + ".txt", "w") saveoutput.write(str(readoutput)) saveoutput.close() #closes the opened file. A closed file cannot be read or written any more. tn.close()
#SCRIPT 8: create nornirscript1.py using NETMIKO from nornir import InitNornir from nornir.plugins.tasks.networking import netmiko_send_command from nornir.plugins.functions.text import print_result nr = InitNornir("config.yml") result = nr.run(netmiko_send_command, command_string="sh ip int brief") print_result(result)
#SCRIPT 9: create nornirscriptt2.py from nornir import InitNornir from nornir.plugins.tasks.networking import napalm_get from nornir.plugins.functions.text import print_result nr = InitNornir("config.yml") result = nr.run(napalm_get,getters=['get_facts']) print_result(result)
#SCRIPT 10: create nornirscript1.py using NETMIKO from nornir import InitNornir from nornir.plugins.tasks.networking import netmiko_send_command from nornir.plugins.functions.text import print_result nr = InitNornir("config.yml") result = nr.run(netmiko_send_command, command_string="sh ip int brief") print_result(result)
#SCRIPT 11: from napalm import get_network_driver driver = get_network_driver('ios') #optional_args123 = {'secret': 'cisco'} #cisco is the enable password #dictionary ios123 = driver('192.168.32.200', 'admin123', 'cisco', optional_args=optional_args123) ios123.open() print(dir(ios123)) ios123.close()
#SCRIPT 12: from napalm import get_network_driver #import json driver123 = get_network_driver('ios') optional_args123 = {'secret': 'cisco'} ios123 = driver123('192.168.32.200', 'admin', 'cisco',optional_args=optional_args123) ios123.open() #start your code here output123 = ios123.get_arp_table() for result123 in output123: print(result123) #stop your code here #dump123 = json.dumps(output123, sort_keys=True, indent=4) #arguments #print(dump123) #with open('arp.txt', 'w') as f: #f.write(dump123) ios123.close()
#SCRIPT 13: import json from pyntc import ntc_device as NTC ios123 = NTC(host='192.168.32.200', username='admin', password='cisco', device_type='cisco_ios_ssh') ios123.open() ios_output = ios123.facts print (json.dumps(ios_output, indent=4)) ios123.close()
#SCRIPT 14: from pyntc import ntc_device as NTC ios123 = NTC(host='192.168.32.132', username='admin', password='cisco', device_type='cisco_ios_ssh') ios123.open() ios123.config("logging host 2.2.2.2") ios_output = ios123.show("show run | i logging") print (ios_output) ios123.close()
#SCRIPT 15: import telnetlib tn123 = telnetlib.Telnet(b"192.168.32.200") tn123.read_until(b"Username: ") tn123.write(b"admin" + b"\n") tn123.read_until(b"Password: ") tn123.write(b"cisco" + b"\n") #telnet established tn123.write(b"show clock\n") print(tn123.read_all().decode("utf8"))
#SCRIPT 16: from netmiko import ConnectHandler #connection = Netmiko(host='10.1.1.1', username='admin', password='cisco', device_type='cisco_ios') cisco_device = { #cisco_devices = called as classes 'device_type': 'cisco_ios', 'ip': '192.168.32.200', 'username': 'admin', 'password': 'cisco', 'port': 22, 'secret': 'cisco123', #no username admin pri 15 // # enable secret cisco123 'verbose':True, } connection123 = ConnectHandler(**cisco_device) #connection123 = object, ** = dictionary used to call function prompt = connection123.find_prompt() print(prompt) if '>' in prompt: connection123.enable() #mode = connection123.check_enable_mode() #print(mode) output = connection123.send_command('show ip int br') print(output) mode = connection123.check_config_mode() #print(mode) if not mode: connection123.config_mode() #mode = connection123.check_config_mode() #print(mode) output = connection123.send_command('username joy password python123') output = connection123.send_command('do show run | i user') print(output)
#SCRIPT 17: from netmiko import ConnectHandler with open('devices.txt') as f123: devices = f123.read().splitlines() #print(devices) device_list = list() #print(device_list) for ip in devices: cisco_device = { 'device_type': 'cisco_ios', 'ip': ip, 'username': 'admin', 'password': 'cisco', 'port': 22, 'secret': 'cisco', #this is the enable password 'verbose': True } device_list.append(cisco_device) #print(device_list) for device in device_list: print('Connecting to ' + device['ip']) connection123 = ConnectHandler(**device) #create connection object print('Entering enable mode ...') connection123.enable() file = input('Enter configuration file (use a valid path) for ' + device['ip'] +':') #enter the configuration file print('Running commands from file:', file, 'to device:', device['ip']) output = connection123.send_config_from_file(file) print(output) connection123.disconnect()
#SCRIPT 18: from netmiko import Netmiko from getpass import getpass net_connect = Netmiko( "192.168.32.200", username="admin1234", password=getpass(), device_type="cisco_ios", ) print(net_connect.find_prompt()) net_connect.disconnect()
#SCRIPT 19: #!/usr/bin/env python """ Use processes and Netmiko to connect to each of the devices. Execute 'show version' on each device. Use a queue to pass the output back to the parent process. Record the amount of time required to do this. """ from __future__ import print_function, unicode_literals from multiprocessing import Process, Queue from datetime import datetime from netmiko import ConnectHandler from my_devices import device_list as devices def show_version_queue(a_device, output_q): """ Use Netmiko to execute show version. Use a queue to pass the data back to the main process. """ output_dict = {} remote_conn = ConnectHandler(**a_device) hostname = remote_conn.base_prompt output = ("#" * 80) + "\n" output += remote_conn.send_command("show ip int br | ex unass") + "\n" output += ("#" * 80) + "\n" output_dict[hostname] = output output_q.put(output_dict) def main(): """ Use processes and Netmiko to connect to each of the devices. Execute 'show version' on each device. Use a queue to pass the output back to the parent process. Record the amount of time required to do this. """ start_time = datetime.now() output_q = Queue(maxsize=20) procs = [] for a_device in devices: my_proc = Process(target=show_version_queue, args=(a_device, output_q)) my_proc.start() procs.append(my_proc) # Make sure all processes have finished for a_proc in procs: a_proc.join() while not output_q.empty(): my_dict = output_q.get() for k, val in my_dict.items(): print(k) print(val) print("\nElapsed time: " + str(datetime.now() - start_time)) if __name__ == "__main__": main()
#SCRIPT 20: #!/usr/bin/env python import threading from datetime import datetime from netmiko import ConnectHandler from my_devices import device_list as devices def show_version(a_device): """Execute show version command using Netmiko.""" remote_conn = ConnectHandler(**a_device) print() print("#" * 80) print(remote_conn.send_command_expect("show version")) print("#" * 80) print() def main(): """ Use threads and Netmiko to connect to each of the devices. Execute 'show version' on each device. Record the amount of time required to do this. """ start_time = datetime.now() for a_device in devices: my_thread = threading.Thread(target=show_version, args=(a_device,)) my_thread.start() main_thread = threading.currentThread() for some_thread in threading.enumerate(): if some_thread != main_thread: print(some_thread) some_thread.join() print("\nElapsed time: " + str(datetime.now() - start_time)) if __name__ == "__main__": main()