def ap_migration(request): if os.path.exists('config_for_wlc.txt'): os.remove('config_for_wlc.txt') # provide list of selected AP: if request.method == "GET": return render(request, 'Migration_Website/ap_migration.html', {'context': ap_to_migration}) if request.method == "POST": wlc_name = request.POST.get['wlc_name'] wlc_ip = request.POST.get['wlc_ip'] config = open('config_for_wlc.txt', 'w+') for ap in ap_to_migration: config.write("config ap primary-base {} {} {} \n ".format(wlc_name, ap, wlc_ip)) config.close() ap_to_migration.clear() # Connect to controller and send commands: cisco1 = { "ip": ip_address "username": username1, "password": password1, "device_type": "cisco_wlc_ssh", 'global_delay_factor': 4, 'banner_timeout': 7 } net_connect = Netmiko(**cisco1) net_connect.send_config_from_file('config_for_wlc.txt') net_connect.disconnect() return HttpResponse("Udało się ?")
def dhcp_pool(ip): show_result("Reading dhcp_pool file...") net_connect = Netmiko(**get_dic(ip)) print("-------------------------------------------------") net_connect.enable() # Envoie la configuration du fichier dhcp_pool net_connect.send_config_from_file("dhcp_pool") show_result("DHCP pool configured !") # Demande de write la conf save = input(show_input("Do you want to write configuration ? (y/n) : ")) if save == "y" or save == "yes": write_conf(ip) end_task() print("-------------------------------------------------")
def main(): my_device = { 'host': 'ios-xe-mgmt.cisco.com', 'username': '******', 'password': '', 'device_type': 'cisco_ios', 'port': 8181 } net_connect = Netmiko(**my_device) # commands = ['int gi3', 'ip addr 1.2.3.4 255.255.255.0'] # # output = net_connect.send_config_set(commands) # print(output) # # output = net_connect.send_command('sh run int gi3') # print(output) # output = net_connect.send_config_from_file('commands_file') print(output) output = net_connect.send_command('sh run int gi3') print(output) net_connect.disconnect()
def Input(ip): my_device = { "host": ip, "username": "******", "password": "******", "device_type": "cisco_ios", } try: net_connect = Netmiko(**my_device) result = net_connect.send_config_from_file(config_file='commands.txt') net_connect.disconnect() return "done" except: return "something wrong"
def config_file(self, host, set_file): ''' this method can be use if you want to write the command in file :param host: IP or hostname the device :param set_file: file location that contain some command :return: ''' device = { 'ip': host, 'username': self.username, 'password': self.password, 'use_keys': self.keys, 'device_type': 'cisco_ios', } net_connect = Netmiko(**device) return net_connect.send_config_from_file(set_file)
def Input(ip): my_device = { "host": ip, "username": "******", "password": "******", "device_type": "cisco_ios", } try: net_connect = Netmiko( **my_device) #Netmiko是一个类,net_connect是一个实例,my_device是产生实例时需要的数据属性 result = net_connect.send_config_from_file( config_file='commands.txt' ) #通过实例去调用类函数,实例本身并没有函数属性,是通过风湿理论去调用类里的函数 net_connect.disconnect() return "done" except: return "something wrong"
def get_prompt(dev): ping_reply = subprocess.call( ['ping', '-c', '3', '-w', '3', '-q', '-n', dev['host']], stdout=subprocess.PIPE) if ping_reply == 0: net_conn = Netmiko(**dev) print("device %s is reachable" % dev['host']) # the commnad file used associated wit the ip address cfg_file = "%s.txt" % dev['host'] output = net_conn.send_config_from_file(cfg_file) print(output) net_conn.save_config() net_conn.disconnect() # its always a good idea to write output to file to make sure that your commands have been executed successfully to the device print( "\nWriting output to file for %s ..please wait while output is printed and saved to File.\n" % dev['host']) with open('cfg-%s.txt' % dev['host'], 'a') as f: f.writelines(output) else: status = "%s is not reachable..skipping" % dev['host'] print(status) pass
'username': '******', 'password': password, 'device_type': 'cisco_ios' } net_conn = Netmiko(**R1) output = net_conn.send_config_set('snmp-server community cisco123') print() print('-' * 80) print(output) print('-' * 80) print() output = net_conn.send_config_from_file('file_commands.txt') print() print('-' * 80) print(output) print('-' * 80) print() output = net_conn.send_command('show run | inc snmp-server') print() print('-' * 80) print(output) print('-' * 80) print()
""" 4. Use send_config_set() and send_config_from_file() to make configuration changes. The configuration changes should be benign. For example, on Cisco IOS I typically change the logging buffer size. As part of your program verify that the configuration change occurred properly. For example, use send_command() to execute 'show run' and verify the new configuration. """ from getpass import getpass from netmiko import Netmiko device = { "host": "sw01.test.domain", "username": "******", "password": getpass(), "device_type": "cisco_s300", } net_conn = Netmiko(**device) output = net_conn.send_config_set("logging buffered 999") output += net_conn.send_config_from_file("config.txt") output += net_conn.send_command("show run") net_conn.disconnect() print(output)
from getpass import getpass password = getpass() csr1000v = { 'device_type': 'cisco_ios', 'username': '******', 'password': password, 'secret': password, 'ip': '192.168.8.136' } connection = Netmiko(**csr1000v) print connection.find_prompt() cmd = ['logging on', 'logging buffered'] print() print('Sending logging config to device') print('-' * 80) output = connection.send_config_set(cmd) #print(output) print('Saving Configuration to NVRAM') print('-' * 80) connection.save_config() output = connection.send_config_from_file('logging_config.txt') print(output) show_run = connection.send_command('show run | include logging') print(show_run) connection.disconnect()
from netmiko import Netmiko username = '' password = '' nxos1 = { 'host': 'nxos1.lasthop.io', 'username': username, 'password': password, 'device_type': 'cisco_nxos', } nxos2 = { 'host': 'nxos2.lasthop.io', 'username': username, 'password': password, 'device_type': 'cisco_nxos', } devices = [nxos1, nxos2] for dev in devices: conn = Netmiko(**dev) output = conn.send_config_from_file('config.txt') conn.save_config() print(output) conn.disconnect()
#!/usr/bin/env python from netmiko import Netmiko from getpass import getpass nxos1 = { 'host': 'nxos1.twb-tech.com', 'username': '******', 'password': getpass(), 'device_type': 'cisco_nxos', } cfg_file = 'config_changes.txt' net_connect = Netmiko(**nxos1) print() print(net_connect.find_prompt()) output = net_connect.send_config_from_file(cfg_file) print(output) print() net_connect.save_config() net_connect.disconnect()
} # Establish SSH connection to the network device using **kwargs to pull in details from dictionary net_connection = Netmiko(**cisco_device) # Send show run command to device to see current logging status and print to console output = net_connection.send_command('show run | inc logging') print(output) # Send command to change logging buffer size net_connection.send_config_set('logging buffered 4000000') # Commit this change on IOS-XR device net_connection.commit() # Send show run command again to verify successful config change and print to console output = net_connection.send_command('show run | inc logging') print('-' * 30) print(output) # This time send command to device from a txt file net_connection.send_config_from_file('config.txt') # Commit this change on IOS-XR device net_connection.commit() # Send show run command again to verify successful config change output = net_connection.send_command('show run | inc logging') # Check if logging buffer size is as we set it via config and print confirmation to console if '4000000' in output: print('Configuration change was successful') else: print('Logging buffer size not correct') print('-' * 30)
def gns3_send_start_config_telnet(gns3_server, project_id, gns3_code_topology_data, global_delay_factor): print(""" ╔═╗┌┬┐┌─┐┌─┐ ╦╦ ╦ ┌─┐┌─┐┌┐┌┌┬┐ ┌─┐┌┬┐┌─┐┬─┐┌┬┐┬ ┬┌─┐ ┌─┐┌─┐┌┐┌┌─┐┬┌─┐ ╚═╗ │ ├┤ ├─┘ ║╚╗╔╝ └─┐├┤ │││ ││ └─┐ │ ├─┤├┬┘ │ │ │├─┘ │ │ ││││├┤ ││ ┬ ╚═╝ ┴ └─┘┴ ╩ ╚╝. └─┘└─┘┘└┘─┴┘ └─┘ ┴ ┴ ┴┴└─ ┴ └─┘┴ └─┘└─┘┘└┘└ ┴└─┘. """) gns3_host_ip = gns3_code_topology_data['gns3_host_ip'] START_CFGS_PATH = gns3_code_topology_data['START_CFGS_PATH'] list_start_config_nodes = [] for node in gns3_code_topology_data['gns3_startup_config_telnet']: name = node['name'] list_start_config_nodes.append(name) for node_name in list_start_config_nodes: print() print( 'Applying startup config to', node_name + ' from', '[' + gns3_code_topology_data['START_CFGS_PATH'] + node_name + ']') print() r_get_nodes = requests.get(gns3_server + '/v2/projects/' + str(project_id) + '/nodes') r_get_nodes_dict = r_get_nodes.json() for dictionary_node in r_get_nodes_dict: if dictionary_node['name'] == node_name: # For VPCS if dictionary_node['node_type'] == 'vpcs': device_type = 'generic_termserver_telnet' config_path = os.path.abspath(START_CFGS_PATH + node_name) telnet_port = dictionary_node['console'] device = { "host": gns3_host_ip, "device_type": device_type, "port": telnet_port, "global_delay_factor": global_delay_factor } net_connect = Netmiko(**device) net_connect.send_config_from_file(config_file=config_path, exit_config_mode=False) net_connect.disconnect() continue # For VyOS. elif dictionary_node['port_name_format'] == 'eth{0}': device_type = 'generic_termserver_telnet' config_path = os.path.abspath(START_CFGS_PATH + node_name) telnet_port = dictionary_node['console'] device = { "host": gns3_host_ip, "device_type": device_type, "port": telnet_port, "global_delay_factor": global_delay_factor } vyos = pexpect.spawn('telnet ' + gns3_host_ip + ' ' + str(telnet_port)) vyos.expect('') vyos.sendline('\n') vyos.expect('login: '******'vyos\n') vyos.expect('Password:'******'vyos') net_connect = Netmiko(**device) net_connect.send_config_from_file(config_file=config_path, exit_config_mode=False) net_connect.disconnect() continue # For JunOS. elif dictionary_node['port_name_format'] == 'ge-0/0/{0}': device_type = 'juniper_junos_telnet' config_path = os.path.abspath(START_CFGS_PATH + node_name) telnet_port = dictionary_node['console'] device = { "host": gns3_host_ip, "device_type": device_type, "port": telnet_port, "global_delay_factor": global_delay_factor } juniper = pexpect.spawn('telnet ' + gns3_host_ip + ' ' + str(telnet_port)) juniper.expect('') juniper.sendline('\n') juniper.expect('login: '******'root\n') juniper.expect('') juniper.sendline('\n') juniper.expect('root@% ', timeout=120) juniper.sendline('cli') with open(config_path) as f: lines = f.read().splitlines() net_connect = Netmiko(**device) net_connect.config_mode() for line in lines: net_connect.send_command_timing(line) time.sleep(1) net_connect.disconnect() continue # For Cisco IOS and Cisco ASA. else: device_type = 'cisco_ios_telnet' config_path = os.path.abspath(START_CFGS_PATH + node_name) telnet_port = dictionary_node['console'] device = { "host": gns3_host_ip, "device_type": device_type, "port": telnet_port, "global_delay_factor": global_delay_factor } net_connect = Netmiko(**device) net_connect.enable() net_connect.send_config_from_file(config_file=config_path) net_connect.disconnect() continue print('Success -', node_name) print('=' * 100)
from netmiko import Netmiko IP_LIST = open('09_devices') for IP in IP_LIST: print ('\n ##### '+ IP.strip() + ' ##### \n' ) RTR = { 'ip': IP, 'username': '******', 'password': '******', 'device_type': 'cisco_ios', } net_connect = Netmiko(**RTR) output = net_connect.send_config_from_file(config_file = '16_config') print(output) output = net_connect.send_command('show ip int brief') print(output)
#!/usr/bin/env python from __future__ import print_function, unicode_literals # Netmiko is the same as ConnectHandler from netmiko import Netmiko from getpass import getpass my_device = { 'host': "host.domain.com", 'username': '******', 'password': getpass(), 'device_type': 'cisco_ios', } net_connect = Netmiko(**my_device) # Make configuration changes using an external file output = net_connect.send_config_from_file("change_file.txt") print(output) net_connect.disconnect()
### Configuring a Networking Device from a File from netmiko import Netmiko connection_object = Netmiko(device_type='cisco_ios', host='192.168.128.129', port=22, username='******', password='******', secret='automation') print(' \n *** Entering Enable Mode *** \n') connection_object.enable() ## Netmiko offers us a method called SEND_CONFIG_FROM_FILE and it send all from that file into SSH connection print(' ... Sending commands from file ospf.txt ...\n') output_var = connection_object.send_config_from_file( 'ospf.txt ') ## the ARGUMENT will be file ospf.txt print(output_var) print(' \n *** Closing the connection *** ') connection_object.disconnect()
from netmiko import Netmiko from getpass import getpass ip_addr = sys.argv[1] username = sys.argv[2] my_device = { 'host': ip_addr, 'username': username, 'password': getpass(), 'device_type': 'cisco_ios' } cfg_commands = ['logging buffered 10000', 'no logging console'] net_conn = Netmiko(**my_device) cfg_cmds_output = net_conn.send_config_set(cfg_commands) cfg_file_output = net_conn.send_config_from_file('config.txt') print(cfg_file_output) sh_run_output = net_conn.send_command('show running-config all | i logging') if cfg_commands[0] in sh_run_output and cfg_commands[1] in sh_run_output: print("Commands committed successfully!") else: print("Commands not committed.") net_conn.disconnect()
#!/usr/bin/env python from netmiko import Netmiko # Sending device ip's stored in a file with open('device_list') as f: device_list = f.read().splitlines() # Iterate through device list and configure the devices for device in device_list: print('Connecting to device ' + device) ip_address_of_device = device # SSH Connection details ios_device = { 'device_type': 'cisco_ios', 'ip': ip_address_of_device, 'username': '******', 'password': '******' } net_connect = Netmiko(**ios_device) output = net_connect.send_config_from_file('reset_config') print(output)
from netmiko import Netmiko my_dev = { 'host': '10.99.236.231', 'username': '******', 'password': '', 'device_type': 'fortinet', } net_conn = Netmiko(**my_dev) # Config cfg_cmds = [ 'config system global', 'set hostname fgvm_test', 'end' ] out = net_conn.send_config_set(cfg_cmds) out = net_conn.send_config_from_file('config_changes.txt') # Check cmd = 'sh sys gl' out = net_conn.send_command(cmd) print(out) net_conn.disconnect()
from netmiko import Netmiko from getpass import getpass #use same password for config and enable mode password = getpass() my_device = { 'host': '10.8.20.100', 'username': '******', 'password': password, 'secret': password, 'device_type': 'cisco_ios' # Use invalid device type to see all supported devices # 'device_type': 'foo' } net_conn = Netmiko(**my_device) output = net_conn.send_config_from_file("change_file.txt") print() print('-' * 80) print(output) print('-' * 80) print()
from netmiko import Netmiko import logging devices = [{ "device_type": "cisco_xe", "ip": "ios-xe-mgmt-latest.cisco.com", "username": "******", "password": "******", "port": "8181", }] logging.basicConfig(filename="test.log", level=logging.DEBUG) logger = logging.getLogger("netmiko") for device in devices: net_connect = Netmiko(**device) output = net_connect.send_config_from_file('changes.txt') print(output) net_connect.disconnect()
from netmiko import Netmiko from credentials import password1, username1 cisco1 = { "host": "10.223.44.102", "username": username1, "password": password1, "device_type": "cisco_ios", } cfg_file = "change_file.txt" net_connect = Netmiko(**cisco1) print() print(net_connect.find_prompt()) net_connect.send_config_from_file(cfg_file) output = net_connect.send_command("sh run | i logging ") print(output) print() # net_connect.save_config() net_connect.disconnect()
inp_file = in_file.readlines() for ip in inp_file: Devices = { 'device_type': 'cisco_ios', 'host': ip, 'username': '******', 'password': '******', 'port': 22, # optional, defaults to 22 'secret': '', # optional, defaults to '' 'auth_timeout': 20 } net_connect = Netmiko(**Devices) time.sleep(20) cfg_file = "configfile.txt" print() print(net_connect.find_prompt()) output = net_connect.enable() output = net_connect.send_config_from_file(cfg_file) print(output) print() net_connect.save_config() net_connect.disconnect() #---------------------------------------------[Code Ends]------------------------------------------------------#
print("vlan " + str(i)) print("name wireless_user-v"+ str(i)) print("tagged 3") for i in range(150,200): print("vlan " + str(i)) print("name server-v"+ str(i)) print("tagged 3") for i in range(200,250): print("vlan " + str(i)) print("name wired_user-v"+ str(i)) print("tagged 3") # close the file, stop redirecting stdout sys.stdout.close() sys.stdout = orig_stdout # setup Netmiko switch = { 'host': '10.139.146.3', 'username': '******', 'password': '******', 'device_type': 'hp_procurve', } switchconnect = Netmiko(**switch) switchconnect.enable() # run Netmiko, print what happens on switch, disconnect output = switchconnect.send_config_from_file("switchfile") print(output) switchconnect.disconnect()
print ('Connecting to device ' + devices) ip_address_of_device = devices ios_device = { 'device_type': 'cisco_ios', 'ip': ip_address_of_device, 'username': username, 'password': password } # Error handling parameters try: net_connect = Netmiko(**ios_device) except (AuthenticationException): print ('Authentication failure: ' + ip_address_of_device) continue except (NetMikoTimeoutException): print ('Timeout to device: ' + ip_address_of_device) continue except (EOFError): print ("End of file while attempting device " + ip_address_of_device) continue except (SSHException): print ('SSH Issue. Are you sure SSH is enabled? ' + ip_address_of_device) continue except Exception as unknown_error: print ('Some other error: ' + str(unknown_error)) continue # Configure the device and save config output = net_connect.send_config_from_file('config_commands') output += net_connect.send_command('wr mem') print (output)
print("ip address 10.1." + str(i) + ".1 255.255.255.0") print("security-level 75") for i in range(200, 250): print("interface GigabitEthernet0/3." + str(i)) print("vlan " + str(i)) print("nameif wired_user-v" + str(i)) print("ip address 10.1." + str(i) + ".1 255.255.255.0") print("security-level 75") # close the file, stop redirecting stdout sys.stdout.close() sys.stdout = orig_stdout # setup Netmiko firewall = { 'host': '10.139.146.1', 'username': '******', 'password': '******', 'device_type': 'cisco_asa', 'secret': 'not_the_real_enable_password_either', } firewallconnect = Netmiko(**firewall) firewallconnect.enable() # run Netmiko, print what happens on firewall, disconnect # https://github.com/ktbyers/netmiko/issues/2025 for why we want that cmd_verify=False here output = firewallconnect.send_config_from_file("firewallfile", cmd_verify=False) print(output) firewallconnect.disconnect()
from getpass import getpass from netmiko import Netmiko password = getpass() device = { 'host': '192.168.122.72', 'username': '******', 'password': password, 'device_type': 'cisco_ios' } ios_commands = ['logging trap alerts', 'spanning-tree portfast bpduguard default'] net_connect = Netmiko(**device) net_connect.send_command_set(ios_commands) net_connect.send_config_from_file('cmd_file')
'secret' : password, 'device_type' : 'cisco_ios', } my_routers = [IOU1, IOU2] net_conn = Netmiko(**IOU1) print(net_conn.find_prompt()) # send_command for execute command not in config mode print(net_conn.send_command("show run | inc hostname")) set1 = ['hostname IOU_1', 'logging console'] #send config from list print(net_conn.send_config_set(set1)) #send from file print("send config from file") print(net_conn.send_config_from_file("D:/Python/Python for NE/Week6/config.txt")) for device_ip in my_routers: net_conn = Netmiko(**device_ip) print(f"----- Interface UP on router {device_ip} -----") print(net_conn.send_command("show ip int bri | inc up"))
'username': '******', 'password': '******', 'secret': 'ENABLE', } cisco_riv202 = { 'device_type': 'cisco_ios', 'ip': '10.214.128.202', 'username': '******', 'password': '******', 'secret': 'ENABLE', } cisco_riv203 = { 'device_type': 'cisco_ios', 'ip': '10.214.128.203', 'username': '******', 'password': '******', 'secret': 'ENABLE', } for cisco_access_riv in (cisco_riv200, cisco_riv201, cisco_riv202, cisco_riv203): net_connect = Netmiko(**cisco_access_riv) net_connect.enable() output = net_connect.send_config_from_file('vlan1000.txt') print((cisco_riv200)output) net_connect.disconnect()
import os from netmiko import Netmiko from getpass import getpass from pprint import pprint from datetime import datetime #password = getpass() hosts = ["cisco1.lasthop.io", "cisco2.lasthop.io"] for host in hosts: device = {'host': host, 'username': '******', 'password': '******', 'device_type': 'cisco_ios', 'session_log': 'logging.txt', 'fast_cli': True} ssh_conn = Netmiko(**device) print(ssh_conn.find_prompt()) print("Configuration of vlan 100-105") sent_vlan = ssh_conn.send_config_from_file("vlans.txt") print("Commit") write_conf = ssh_conn.save_config() print(write_conf)
except NameError: host = input("Enter host to connect to: ") password = getpass() device = { 'host': host, 'username': '******', 'password': password, 'device_type': 'cisco_ios', "secret": "cisco" } net_connect = Netmiko(**device) net_connect.enable() # Use send_config_set() to make config change config = ['logging console', 'logging buffer 15000'] output = net_connect.send_config_set(config) output_printer(output) # Use send_config_from_file() to make config change output = net_connect.send_config_from_file('config.txt') output_printer(output) message = "Verifying config change\n" output = net_connect.send_command("show run | inc logging") if '8000' in output: message += "Logging buffer is size 8000" else: message += "Logging buffer size is not correct!" output_printer(message)
except NameError: host = input("Enter host to connect to: ") password = getpass() device = { 'host': host, 'username': '******', 'password': password, 'device_type': 'cisco_ios', } net_connect = Netmiko(**device) # Use send_config_set() to make config change config = ['logging console', 'logging buffer 15000'] output = net_connect.send_config_set(config) output_printer(output) # Use send_config_from_file() to make config change output = net_connect.send_config_from_file('config.txt') output_printer(output) message = "Verifying config change\n" output = net_connect.send_command("show run | inc logging") if '8000' in output: message += "Logging buffer is size 8000" else: message += "Logging buffer size is not correct!" output_printer(message)