【问题标题】:Terminal Server Netmiko Connection终端服务器 Netmiko 连接
【发布时间】:2020-03-06 14:39:36
【问题描述】:

我正在尝试进行自动部署,该部署将提供给设备的远程配置,但我不知道出了什么问题。

我不是专家,只是学习 Python 的初学者。

我的设置。

PythonServer--ssh-> Cisco Console Server --reverse 原则 --> New_device(cisco)

我可以成功连接到 cisco colse 服务器。 (简单的部分,我可以使用 device_type 作为 'cisco_ios'、'cisco_ios_telnet'、terminal_server'、'generic_terminal_server' 进行连接)所有这些都可以工作。

当我尝试反向 telnet 时,我只收到密码提示。

************************************************************************
This is a privately owned computing system.  Access permitted only by
authorized employees.
************************************************************************
Password: 

我无法克服这一点。这是我的代码。我究竟做错了什么? (我认为这与 netmiko 处理连接的方式有关)有人可以帮我解决这个问题。

import time
from netmiko import ConnectHandler, redispatch

conserver_username = 'cisco'
conserver_password = 'cisco'
conserver_ip = '10.88.77.152'

console_server = {
    'host': conserver_ip,
    'username': conserver_username,
    'password': conserver_password,
    'device_type': 'cisco_ios',
    'session_log': 'log.out'
}

net_connect = ConnectHandler(**console_server)

net_connect.send_command('\n', expect_string=r'#')
net_connect.send_command('telnet 10.88.77.152 2004', expect_string=r':')
net_connect.send_command_timing('cisco')
#net_connect.send_command_timing('cisco')
net_connect.send_command_timing('\n')

这会用横幅提示我,但不会继续。

我相信该模块会查找用户名,但由于它没有显示用户名,因此不知道如何处理它。

【问题讨论】:

    标签: python network-programming netmiko


    【解决方案1】:

    可能有两个问题:

    1. 如果设备在登录时不需要密码(密码在 vty 行中配置)。这将是 Cisco ios 上的配置问题
    2. 尝试使用 write_channel 代替 send_command 来建立连接。您可能想尝试下面的代码,我添加了一些打印来帮助调试。另外,请检查您的日志文件:
    import time
    from netmiko import ConnectHandler, redispatch
    
    conserver_username = 'cisco'
    conserver_password = 'cisco'
    conserver_ip = '10.88.77.152'
    
    console_server = {
        'host': conserver_ip,
        'username': conserver_username,
        'password': conserver_password,
        'device_type': 'cisco_ios',
        'session_log': 'log.out'
    }
    
    new_device_username = 'cisco'
    new_device_password = 'cisco'
    
    net_connect = ConnectHandler(**console_server)
    
    #to make sure that you are in logged in to the right device:
    output = net_connect.find_prompt()
    print(output)
    # write channel
    net_connect.write_channel('telnet 10.88.77.152 2004')
    time.sleep(2) # this is needed for the device to send response. Yoiu may adjust timing depending on your end device
    output = net_connect.read_channel()
    print(output)
    
    #send username:
    net_connect.write_channel(new_device_username)
    time.sleep(2)
    #send password:
    net_connect.write_channel(new_device_password)
    time.sleep(2)
    output = net_connect.read_channel()
    print(output)
    
    #see if you are logged in
    net_connect.write_channel('\n')
    time.sleep(1)
    output = net_connect.read_channel()
    print(output)
    #redispatch when logged in and use as normal:
    redispatch(net_connect, device_type='cisco_ios_telnet')
    output = net_connect.find_prompt()
    print(output)
    

    注意:有redispatch方法!

    你可能想看看this关于代理的文章

    【讨论】:

      【解决方案2】:

      你可能想要添加

      net_connect.write_channel("\r\n")
      

      输入用户名和密码后提供返回/回车键功能。

      net_connect.write_channel(new_device_username)
      time.sleep(2)
      net_connect.write_channel("\r\n")
      

      net_connect.write_channel(new_device_password)
      time.sleep(2)
      net_connect.write_channel("\r\n")
      

      【讨论】: