Exemple #1
0
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()
Exemple #2
0
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!*")
Exemple #3
0
# SCRIPT 3 NETMIKO SCRIPT TO CONNECT “TERMINAL SERVER” FIRST AND THEN GOTO SSH CISCO_IOS

    import time
    from netmiko import ConnectHandler, redispatch

    net_connect = ConnectHandler(
        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
Exemple #4
0
# Establish SSH connection to the network device using **kwargs to pull in details from dictionary
net_connect = Netmiko(**device)

# 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()




Exemple #5
0
#!/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()
Exemple #6
0
router_ip = '192.168.0.50'
router_user = '******'
router_password = '******'

print(f"\n{'#'*50}\n1. Connecting to the Cisco Jump host\n{'#'*50}")
try:
	net_connect = Netmiko(device_type='cisco_ios',
								 host = cisco_jump_host_ip,
								 username = cisco_jump_host_user,
								 password = cisco_jump_host_password)

	print(net_connect.find_prompt())
	print(f"\n{'*'*10}Connected to the Jump host{'*'*10}")
	print(f"\n{'~'*50}\n2. Connecting to the Dest Router\n{'~'*50}")
	print(net_connect.find_prompt(),end='')
	net_connect.write_channel(f'ssh -l {router_user} {router_ip}\n')
	time.sleep(2)
	output = net_connect.read_channel()
	print(output)
	if 'Password' in output:
		print("**Received Password Prompt, Entering password**")
		net_connect.write_channel(router_password+'\n')
		time.sleep(2)

		print(f"\n{'~'*50}\nDestination Device Prompt\n{'~'*50}\n")
		print(net_connect.find_prompt())
		cmds = ['show ip int brie','show ip route','show users']

		redispatch(net_connect,device_type='cisco_ios')
		for cmd in cmds:
			print(f"\n{'~'*50}\nIn {net_connect.find_prompt()} Executing the Command: {cmd}\n{'~'*50}")
    "password": "******",
    "secret": "ENABLE_PASSWORD",
    "device_type": "cisco_ios",
}

sw_list = [ARUBA_7005, ARUBA_7010, ARUBA_7030]

for device in sw_list:
    response = os.system(
        "ping -c 1 " +
        device['ip'])  # check whether the switch is UP using ICMP echo request
    if response == 0:
        print('WLAN Controller with ip address ', device['ip'], ' is UP')
        net_connect = Netmiko(**device)
        print(net_connect.find_prompt())
        print('Initiating backup of : ', device['ip'], ' WLAN Controller')
        #net_connect.enable()
        #print(net_connect.find_prompt())
        net_connect.write_channel('no paging')
        output = net_connect.send_command("show running-config")
        filename1 = time.strftime("%Y%m%d-%H%M%S")
        filename2 = device['ip']
        completeName = filename2 + "_" + filename1 + "_cont.txt"
        with open(os.path.join('/PATH/TO/STORE/BACKUP/FILES', completeName),
                  'w') as f:
            print(output, file=f)
        print('completed backup of : ', device['ip'], ' WLAN Controller')
        net_connect.disconnect()
    else:
        print('WLAN Controller with ip address ', device['ip'], ' is DOWN')
Exemple #8
0
#!/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))
Exemple #9
0
        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()
Exemple #10
0
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()
Exemple #11
0
print("-" * 79)
print(ssh_conn.find_prompt())
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())
Exemple #12
0
#!/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()