def confgen(vars): # Generate configuration lines with Jinja2 with open(jinja_template) as f: tfile = f.read() template = jinja2.Template(tfile) cfg_list = template.render(vars) print('-' * 80) print(cfg_list) print('-' * 80) # Connect directly to host via telnet on the specified port conn = Netmiko(host=hostip, device_type='cisco_ios_telnet', port=vars['port']) # Check if host is in initial config state conn.write_channel("\n") time.sleep(1) output = conn.read_channel() if 'initial configuration dialog' in output: conn.write_channel('no\n') time.sleep(1) # Send generated commands to host output = conn.enable() output = conn.send_config_set(cfg_list) # Display results print('-' * 80) print('\nConfiguration applied on ' + vars['host'] + ': \n\n' + output) print('-' * 80) # Probably a good idea conn.disconnect()
def simpleConnect(ip): # Values within need to be changed for functionality! node = { "host": (ip), "username": userName, "password": userPass, "device_type": "cisco_ios",} try: net_connect = Netmiko(**node) hostname = (net_connect.find_prompt()) net_connect.find_prompt() sleep(1) if '#' not in (net_connect.find_prompt()): # Checking for enable mode net_connect.write_channel("en" + "\r") net_connect.write_channel(userPass + "\r") # Passing in credentials for enable mode print(net_connect.read_channel()) sleep(1) print(net_connect.read_channel()) output = net_connect.send_command('show inventory') # Change me and ball-out sleep(1) print(output) if "2960x" in output: # Change me and ball-out print ("\n\n\n\t\t\t" +hostname+ " is a 2960x model switch!") results.write(hostname+ " is a 2960x model switch!\n") sleep(1) checkfunc = net_connect.send_command('test interfaces vlan 1') print (checkfunc) if "No IP address" in checkfunc: # Change me and ball-out print ("\n\t" + hostname + " VLAN 1 has no IP address!\n") results.write(hostname +" VLAN 1 has no IP address!\n") else: pass if "2901" in output: # Change me and ball-out print ("\n\n\n\t\t\t" +hostname+ " is a 2901 router!") results.write(hostname+ " is a 2901 router!\n") sleep(1) if "% Invalid" in output: # Change me and ball-out print("Syntax Error! Check compatibility for CLI command!") results.write(hostname + " experienced a ***Syntax Error!***\n") sleep(1) net_connect.disconnect() except: print("*Authentication Error!*")
device_type="terminal_server", ip="192.168.32.131", username="******", password="******", ) output = net_connect.send_command("ifconfig") print(output) net_connect.write_channel("ssh -l admin 192.168.32.132\r\n") time.sleep(1) max_loops = 10 i = 5 while i <= max_loops: output = net_connect.read_channel() if "Password" in output: net_connect.write_channel(net_connect.password + "\r\n") time.sleep(0.5) output = net_connect.read_channel() if ">" in output or "#" in output: break time.sleep(0.5) i += 1 redispatch(net_connect, device_type="cisco_ios") new_output = net_connect.send_command("show ip int brief") print(new_output)
# Part a - find and print the prompt to the screen print(net_connect.find_prompt()) # Part B - Enter config mode and find the prompt print(net_connect.config_mode()) print(net_connect.find_prompt()) # Part C - exit config mode and print the prompt print(net_connect.exit_config_mode()) print(net_connect.find_prompt()) # Part D - send disable down the SSH channel print(net_connect.write_channel('disable\n')) # Part E - Sleep for 2s and read back from SSH channel time.sleep(2) print(net_connect.read_channel()) # Part F - Go into enable mode and print prompt print(net_connect.enable()) print(net_connect.find_prompt()) net_connect.disconnect()
#!/usr/bin/env python3 from netmiko import Netmiko from getpass import getpass import logging import time logging.basicConfig(filename='test.log', level=logging.DEBUG) logger = logging.getLogger("netmiko") # Credential in GNS3 : admin, admin123 password = getpass(prompt="Password : "******"show ip arp") print(output) print(type(output)) net_conn.write_channel("show ip arp\n") time.sleep(15) outout = net_conn.read_channel("show ip arp\n") print(output) print(type(output))
inventory['device_type'] = vals['Device type'] inv_list.append(inventory.copy()) for items in inv_list: print('\nCurrent device: ' + items['device']) with open(jinja_template) as f: tfile = f.read() template = jinja2.Template(tfile) cfg_list = template.render(items).split('\n') conn = Netmiko(host=items['ip'], device_type=items['device_type'], username=items['username'], password=items['password'], port=items['port']) conn.write_channel("\n") time.sleep(1) output = conn.read_channel() if 'initial configuration dialog' in output: conn.write_channel('no\n') time.sleep(1) output = conn.enable() output = conn.send_config_set(cfg_list) print(output) conn.disconnect()
password = getpass() device = { "host": "cisco4.lasthop.io", "username": "******", "password": password, "secret": password, "device_type": "cisco_ios", "session_log": "my_output_l2-ex6.txt", } net_connection = Netmiko(**device) print(net_connection.find_prompt()) net_connection.config_mode() print(net_connection.find_prompt()) net_connection.exit_config_mode() print(net_connection.find_prompt()) net_connection.write_channel("disable\n") time.sleep(2) output = net_connection.read_channel() print(output) print("Enter enable mode") net_connection.enable() print(net_connection.find_prompt()) net_connection.disconnect() print()
print("-" * 79) print("Execution the config_mode:") print("-" * 79) ssh_conn.config_mode() print(ssh_conn.find_prompt()) print("-" * 79) print("Execution the exit_config_mode:") print("-" * 79) ssh_conn.exit_config_mode() print(ssh_conn.find_prompt()) print("-" * 79) print("Execution the write_channel:") print("-" * 79) ssh_conn.write_channel('disable') print(ssh_conn.find_prompt()) print("-" * 79) print("Execution the time sleep command:") time.sleep(2) print("-" * 79) print("Execution the read_channel:") print("-" * 79) ssh_conn.read_channel() print(ssh_conn.find_prompt()) print("-" * 79) print("Execution the enable mode:") print("-" * 79) ssh_conn.read_channel() ssh_conn.enable() print(ssh_conn.find_prompt()) print("-" * 79)
#!/usr/bin/env python from netmiko import Netmiko from getpass import getpass import time cisco1 = { 'host': 'cisco1.twb-tech.com', 'username': '******', 'password': getpass(), 'device_type': 'cisco_ios', } net_connect = Netmiko(**cisco1) print(net_connect.find_prompt()) net_connect.write_channel("show ip int brief\n") time.sleep(1) output = net_connect.read_channel() print(output) net_connect.disconnect()