当前位置: 首页>>代码示例>>Python>>正文


Python netmiko.ConnectHandler方法代码示例

本文整理汇总了Python中netmiko.ConnectHandler方法的典型用法代码示例。如果您正苦于以下问题:Python netmiko.ConnectHandler方法的具体用法?Python netmiko.ConnectHandler怎么用?Python netmiko.ConnectHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在netmiko的用法示例。


在下文中一共展示了netmiko.ConnectHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    """
    Use Netmiko to change the logging buffer size and to disable console logging
    from a file for both pynet-rtr1 and pynet-rtr2
    """
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    for a_device in (pynet1, pynet2):
        net_connect = ConnectHandler(**a_device)
        net_connect.send_config_from_file(config_file='config_file.txt')

        # Verify configuration
        output = net_connect.send_command("show run | inc logging")
        print()
        print('#' * 80)
        print("Device: {}:{}".format(net_connect.ip, net_connect.port))
        print()
        print(output)
        print('#' * 80)
        print() 
开发者ID:ktbyers,项目名称:python_course,代码行数:27,代码来源:ex8_netmiko.py

示例2: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    """Use Netmiko to execute 'show arp' on pynet-rtr1, pynet-rtr2, and juniper-srx."""
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    print("\nStart time: " + str(datetime.now()))
    for a_device in (pynet1, pynet2, juniper_srx):
        net_connect = ConnectHandler(**a_device)
        output = net_connect.send_command("show arp")
        print()
        print('#' * 80)
        print("Device: {}:{}".format(net_connect.ip, net_connect.port))
        print()
        print(output)
        print('#' * 80)
        print()

    print("\nEnd time: " + str(datetime.now())) 
开发者ID:ktbyers,项目名称:python_course,代码行数:24,代码来源:ex6_netmiko.py

示例3: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    """Use Netmiko to change the logging buffer size on pynet-rtr2."""
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    net_connect = ConnectHandler(**pynet2)
    config_commands = ['logging buffered 20000']
    net_connect.send_config_set(config_commands)

    output = net_connect.send_command("show run | inc logging buffer")
    print()
    print('#' * 80)
    print("Device: {}:{}".format(net_connect.ip, net_connect.port))
    print()
    print(output)
    print('#' * 80)
    print() 
开发者ID:ktbyers,项目名称:python_course,代码行数:23,代码来源:ex7_netmiko.py

示例4: show_version_queue

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
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 = {}
    creds = a_device.credentials
    remote_conn = ConnectHandler(device_type=a_device.device_type,
                                 ip=a_device.ip_address,
                                 username=creds.username, password=creds.password,
                                 port=a_device.port, secret='', verbose=False)

    output = ('#' * 80) + "\n"
    output += remote_conn.send_command_expect("show version") + "\n"
    output += ('#' * 80) + "\n"
    output_dict[a_device.device_name] = output
    output_q.put(output_dict) 
开发者ID:ktbyers,项目名称:python_course,代码行数:19,代码来源:ex8_proc_w_queue.py

示例5: show_version

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def show_version(a_device):
    '''
    Execute show version command using Netmiko
    '''
    creds = a_device.credentials
    remote_conn = ConnectHandler(device_type=a_device.device_type,
                                 ip=a_device.ip_address,
                                 username=creds.username,
                                 password=creds.password,
                                 port=a_device.port, secret='')
    print()
    print('#' * 80)
    print(remote_conn.send_command_expect("show version"))
    print('#' * 80)
    print()
    remote_conn.disconnect() 
开发者ID:ktbyers,项目名称:python_course,代码行数:18,代码来源:ex6_threads_show_ver.py

示例6: connect_key

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def connect_key(self, params={}):
        home_dir = (path.expanduser('~'))
        key_file = "{}/.ssh".format(home_dir)
        f = params.get('key').get('privateKey')
        fb = f.get('content')
        fb64 = base64.b64decode(fb)
        fb64 = fb64.decode("utf-8")
        if not path.exists(key_file):
            os.makedirs(key_file)
            os.chmod(key_file, 0o700)
        key_file_path = path.join(key_file, "id_rsa")
        with open(key_file_path, 'w+') as f:
            f.write(fb64)
        os.chmod(key_file_path, 0o600)
        self.logger.info("Establishing connection")
        device = {'device_type': params.get('device_type'), 'ip': params.get('host'),
                  'username': params.get('credentials').get('username'), 'use_keys': True, 'key_file': key_file_path,
                  'password': params.get('credentials').get('password'), 'port': params.get('port'),
                  'secret': params.get('secret').get('secretKey'), 'allow_agent': True, 'global_delay_factor': 4}
        self.device_connect = ConnectHandler(**device)
        return self.device_connect 
开发者ID:rapid7,项目名称:insightconnect-plugins,代码行数:23,代码来源:connection.py

示例7: switch_interface

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def switch_interface(device_id: str, interface_name: str, enable_interface: str) -> Any:
    device = Device.objects.get(pk=device_id)
    config_commands = [f'interface {interface_name}']
    result = {"interface_name": interface_name}
    if enable_interface == 'False':
        config_commands.append(' shutdown')
        result["up"] = False
    else:
        config_commands.append(' no shutdown')
        result["up"] = True
    conn_params = {
        'ip': device.host,
        'username': device.username,
        'password': device.password,
        'device_type': device.netmiko_device_type,
    }
    with ConnectHandler(**conn_params) as device_conn:
        device_conn.send_config_set(config_commands)
    return result 
开发者ID:dmfigol,项目名称:network-programmability-stream,代码行数:21,代码来源:tasks.py

示例8: collect_outputs

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def collect_outputs(devices, commands):
    """
    Collects commands from the dictionary of devices

    Args:
        devices (dict): dictionary, where key is the hostname, value is
            netmiko connection dictionary
        commands (list): list of commands to be executed on every device

    Returns:
        dict: key is the hostname, value is string with all outputs
    """
    for device in devices:
        hostname = device.pop("hostname")
        connection = netmiko.ConnectHandler(**device)
        device_result = ["{0} {1} {0}".format("=" * 20, hostname)]

        for command in commands:
            command_result = connection.send_command(command)
            device_result.append("{0} {1} {0}".format("=" * 20, command))
            device_result.append(command_result)

        device_result_string = "\n\n".join(device_result)
        connection.disconnect()
        yield device_result_string 
开发者ID:dmfigol,项目名称:network-programmability-stream,代码行数:27,代码来源:gather_commands_sync.py

示例9: netmiko_connect

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def netmiko_connect(request):
    """Connect to arista1 and return connection object"""
    password = os.getenv("PYNET_PASSWORD") if os.getenv("PYNET_PASSWORD") else getpass()
    arista1 = {
        "device_type": "arista_eos",
        "host": "arista1.lasthop.io",
        "username": "pyclass",
        "password": password,
    }

    net_connect = ConnectHandler(**arista1)

    def fin():
        net_connect.disconnect()

    request.addfinalizer(fin)
    return net_connect 
开发者ID:ktbyers,项目名称:pyplus_course,代码行数:19,代码来源:conftest.py

示例10: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    '''
    Use Netmiko to execute 'show arp' on pynet-rtr1, pynet-rtr2, and juniper-srx
    '''
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    print "\nStart time: " + str(datetime.now())
    for a_device in (pynet1, pynet2, juniper_srx):
        net_connect = ConnectHandler(**a_device)
        output = net_connect.send_command("show arp")
        print
        print '#' * 80
        print "Device: {}:{}".format(net_connect.ip, net_connect.port)
        print
        print output
        print '#' * 80
        print

    print "\nEnd time: " + str(datetime.now()) 
开发者ID:ktbyers,项目名称:pynet,代码行数:26,代码来源:ex6_netmiko.py

示例11: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    '''
    Use Netmiko to change the logging buffer size on pynet-rtr2.
    '''
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    net_connect = ConnectHandler(**pynet2)
    config_commands = ['logging buffered 20000']
    net_connect.send_config_set(config_commands)

    output = net_connect.send_command("show run | inc logging buffer")
    print
    print '#' * 80
    print "Device: {}:{}".format(net_connect.ip, net_connect.port)
    print
    print output
    print '#' * 80
    print 
开发者ID:ktbyers,项目名称:pynet,代码行数:25,代码来源:ex7_netmiko.py

示例12: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    '''
    Using Netmiko enter into configuration mode on a network device.

    Verify that you are currently in configuration mode.
    '''
    password = getpass()

    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
    net_connect2 = ConnectHandler(**pynet2)
    net_connect2.config_mode()
    print "\n>>>>"
    print "Checking pynet-rtr2 is in configuration mode."
    print "Config mode check: {}".format(net_connect2.check_config_mode())
    print "Current prompt: {}".format(net_connect2.find_prompt())
    print ">>>>\n" 
开发者ID:ktbyers,项目名称:pynet,代码行数:19,代码来源:ex5_netmiko.py

示例13: worker_cmd

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def worker_cmd(a_device, mp_queue, cmd='show arp'):
    '''
    Return a dictionary where the key is the device identifier
    Value is (success|fail(boolean), return_string)
    '''
    identifier = '{ip}:{port}'.format(**a_device)
    return_data = {}

    try:
        net_connect = ConnectHandler(**a_device)
        output = net_connect.send_command(cmd)
        return_data[identifier] = (True, output)
    except (NetMikoTimeoutException, NetMikoAuthenticationException) as e:
        return_data[identifier] = (False, e)

    # Add data to the queue (for parent process)
    mp_queue.put(return_data) 
开发者ID:ktbyers,项目名称:pynet,代码行数:19,代码来源:ex9_netmiko.py

示例14: main

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def main():
    '''
    Use Netmiko to change the logging buffer size and to disable console logging
    from a file for both pynet-rtr1 and pynet-rtr2
    '''
    password = getpass()

    # Get connection parameters setup correctly
    for a_dict in (pynet1, pynet2, juniper_srx):
        a_dict['password'] = password
        a_dict['verbose'] = False

    for a_device in (pynet1, pynet2):
        net_connect = ConnectHandler(**a_device)
        net_connect.send_config_from_file(config_file='config_file.txt')

        # Verify configuration
        output = net_connect.send_command("show run | inc logging")
        print
        print '#' * 80
        print "Device: {}:{}".format(net_connect.ip, net_connect.port)
        print
        print output
        print '#' * 80
        print 
开发者ID:ktbyers,项目名称:pynet,代码行数:27,代码来源:ex8_netmiko.py

示例15: show_version

# 需要导入模块: import netmiko [as 别名]
# 或者: from netmiko import ConnectHandler [as 别名]
def show_version(a_device):
    '''
    Execute show version command using Netmiko
    '''
    creds = a_device.credentials
    remote_conn = ConnectHandler(device_type=a_device.device_type,
                                 ip=a_device.ip_address,
                                 username=creds.username,
                                 password=creds.password,
                                 port=a_device.port, secret='')
    print
    print '#' * 80
    print remote_conn.send_command_expect("show version")
    print '#' * 80
    print
    remote_conn.disconnect() 
开发者ID:ktbyers,项目名称:pynet,代码行数:18,代码来源:ex6_threads_show_ver.py


注:本文中的netmiko.ConnectHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。