How to use netmiko - 10 common examples

To help you get started, we’ve selected a few netmiko examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ktbyers / pyplus_course / bonus2 / collateral / PYTEST / test3 / test_netmiko.py View on Github external
def netmiko_conn():
    net_connect = ConnectHandler(
        host="cisco3.lasthop.io",
        device_type="cisco_ios",
        username="pyclass",
        password=getpass(),
    )
    return net_connect
github ktbyers / netmiko / tests / unit / test_base_connection.py View on Github external
#!/usr/bin/env python

import time
from os.path import dirname, join
from threading import Lock

from netmiko import NetMikoTimeoutException
from netmiko.base_connection import BaseConnection

RESOURCE_FOLDER = join(dirname(dirname(__file__)), "etc")


class FakeBaseConnection(BaseConnection):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)
        self._session_locker = Lock()


def test_timeout_exceeded():
    """Raise NetMikoTimeoutException if waiting too much"""
    connection = FakeBaseConnection(session_timeout=10)
    start = time.time() - 11
    try:
        connection._timeout_exceeded(start)
    except NetMikoTimeoutException as exc:
        assert isinstance(exc, NetMikoTimeoutException)
        return
github ktbyers / netmiko / tests / old_format / test_cisco_legacy.py View on Github external
def setup_module(module):

    module.EXPECTED_RESPONSES = {
        'base_prompt' : 'pynet-rtr1',
        'interface_ip'  : '10.220.88.20'
    }
    
    show_ver_command = 'show version'
    module.basic_command = 'show ip int brief'
   
    SSHClass = netmiko.ssh_dispatcher(device_type=cisco_881['device_type']) 
    net_connect = SSHClass(**cisco_881)

    module.show_version = net_connect.send_command(show_ver_command)
    module.show_ip = net_connect.send_command(module.basic_command)
    module.base_prompt = net_connect.base_prompt

    module.show_ip_alt = net_connect.send_command_expect(module.basic_command)
    module.show_version_alt = net_connect.send_command_expect(show_ver_command)

    # Test buffer clearing
    net_connect.remote_conn.sendall(show_ver_command)
    time.sleep(2)
    net_connect.clear_buffer()
    # Should not be anything there on the second pass
    module.clear_buffer_check = net_connect.clear_buffer()
github ktbyers / netmiko / tests / test_f5_ltm.py View on Github external
def setup_module(module):

    module.EXPECTED_RESPONSES = {
        'device_type'   : 'BIG-IP',
        'pool_name'     : 'Ltm::Pool: TEST',
    }
    
    show_sys_command = 'tmsh show sys version'
    multiple_line_command = 'tmsh show sys log ltm'
    module.basic_command = 'tmsh show ltm pool TEST'
    
    SSHClass = netmiko.ssh_dispatcher(f5_ltm_1['device_type'])
    net_connect = SSHClass(**f5_ltm_1)
    module.show_version = net_connect.send_command(show_sys_command)
    module.multiple_line_output = net_connect.send_command(multiple_line_command, delay_factor=2)
    module.show_pool = net_connect.send_command(module.basic_command)
github ktbyers / netmiko / tests / test_cisco_xe_enable.py View on Github external
def setup_module(module):

    module.EXPECTED_RESPONSES = {
        'enable_prompt' : 'xe-test-rtr#',
        'base_prompt'   : 'xe-test-rtr',
        'interface_ip'  : '172.30.0.167',
        'config_mode'   : '(config)',
    }
    
    show_ver_command = 'show version'
    module.basic_command = 'show ip int brief'
    
    SSHClass = netmiko.ssh_dispatcher(cisco_xe['device_type'])
    net_connect = SSHClass(**cisco_xe)
    module.show_version = net_connect.send_command(show_ver_command)
    module.show_ip = net_connect.send_command(module.basic_command)

    net_connect.enable()
    module.enable_prompt = net_connect.find_prompt()

    module.config_mode = net_connect.config_mode()

    config_commands = ['logging buffered 20000', 'logging buffered 20010', 'no logging console']
    net_connect.send_config_set(config_commands)

    module.exit_config_mode = net_connect.exit_config_mode()

    module.config_commands_output = net_connect.send_command('show run | inc logging buffer')
github ktbyers / netmiko / tests / test_cisco_xe.py View on Github external
def setup_module(module):

    module.EXPECTED_RESPONSES = {
        'base_prompt' : 'xe-test-rtr',
        'interface_ip'  : '172.30.0.167',
    }
    
    show_ver_command = 'show version'
    module.basic_command = 'show ip int brief'
    
    SSHClass = netmiko.ssh_dispatcher(cisco_xe['device_type'])
    net_connect = SSHClass(**cisco_xe)
    module.show_version = net_connect.send_command(show_ver_command)
    module.show_ip = net_connect.send_command(module.basic_command)
    module.base_prompt = net_connect.base_prompt
github ktbyers / netmiko / tests / test_hp_comware.py View on Github external
def setup_module(module):

    module.EXPECTED_RESPONSES = {
        'base_prompt'   : 'vsr1000',
        'router_prompt' : '',
        'interface_ip'  : '192.168.112.11',
        'config_mode'   : '[vsr1000]',
    }
    
    show_ver_command = 'display version'
    multiple_line_command = 'display logbuffer'
    module.basic_command = 'display ip interface brief'
    
    SSHClass = netmiko.ssh_dispatcher(hp_comware['device_type'])
    net_connect = SSHClass(**hp_comware)

    module.show_version = net_connect.send_command(show_ver_command)
    module.multiple_line_output = net_connect.send_command(multiple_line_command, delay_factor=2)
    module.show_ip = net_connect.send_command(module.basic_command)

    module.base_prompt = net_connect.base_prompt

    # Enter config mode
    module.config_mode = net_connect.config_mode()
    
        # Exit config mode
    module.exit_config_mode = net_connect.exit_config_mode()

    # Send a set of config commands
    config_commands = ['vlan 3000', 'name 3000-test']
github ktbyers / netmiko / tests / test_netmiko_scp.py View on Github external
def test_file_transfer(scp_file_transfer):
    """Test Netmiko file_transfer function."""
    ssh_conn, file_system = scp_file_transfer
    source_file = "test9.txt"
    dest_file = "test9.txt"
    direction = "put"

    transfer_dict = file_transfer(
        ssh_conn,
        source_file=source_file,
        dest_file=dest_file,
        file_system=file_system,
        direction=direction,
        overwrite_file=True,
    )

    # No file on device at the beginning
    assert (
        transfer_dict["file_exists"]
        and transfer_dict["file_transferred"]
        and transfer_dict["file_verified"]
    )

    # File exists on device at this point
github ktbyers / netmiko / tests / unit / test_utilities.py View on Github external
def test_string_to_bytes():
    """Convert string to bytes"""
    assert utilities.write_bytes("test") == b"test"
github ktbyers / netmiko / tests / unit / test_utilities.py View on Github external
def test_bytes_to_bytes():
    """Convert bytes to bytes"""
    result = b"hello world"
    assert utilities.write_bytes(result) == result