【问题标题】:Python - connect to network device through Terminal using PasswordPython - 使用密码通过终端连接到网络设备
【发布时间】:2019-08-03 20:02:37
【问题描述】:

我的目标是通过代理/终端服务器连接到网络设备。当我使用 Putty 连接时,我输入:

用户名:ttyS1@终端服务器名

其中 username 是用户名 :ttyS 后面的数字是我们要连接的端口号,terminalservername 是代理服务器的名称。我已经尝试了从这两个链接Link1Link2得到的代码

from netmiko import ConnectHandler
import time
from netmiko import redispatch

jumpserver = {'device_type': 'terminal_server','ip': 'x.x.x.x','username': 'name','password': 'pass','global_delay_factor':5}

net_connect = ConnectHandler(**jumpserver)
print net_connect.find_prompt()

net_connect.write_channel('command to access router')
time.sleep(1)
net_connect.read_channel()

redispatch(net_connect, device_type='arista_eos')
net_connect.send_command('show hostname')

当我运行脚本时,它已经在终端服务器上,无法进一步连接到网络设备。

有人可以建议如何像 PUTTY 那样通过代理/终端服务器连接到网络设备并获取设备的主机名。

我从我的一位同事那里得到了以下代码。我现在可以访问连接在特定端口上的设备,但无法登录。当我在登录提示符处输入用户名时,它要求输入旧密码,然后再次要求输入密码。我不明白为什么会发生这种情况,因为我可以使用 Putty 成功使用相同的帐户和密码登录。只有通过 Python 脚本,它才会询问旧密码,然后无限期地询问密码。代码如下:

import time
from netmiko import ConnectHandler, redispatch

zpe_username = "serviceaccount"
zpe_password = "xxxxxxx"
zpe_hostname = "TerminalServerName"
console_username = zpe_username + ":ttyS" + "1"
console_server = {
    "host": zpe_hostname,
    "username": console_username,
    "password": zpe_password,
    "device_type": "terminal_server",
}
print("ssh " + console_username + "@" + zpe_hostname)

net_connect = ConnectHandler(**console_server)
net_connect.write_channel(zpe_username + "\n")
time.sleep(1)
password_prompt = net_connect.read_channel()
net_connect.write_channel(zpe_password + "\n")
time.sleep(1)

redispatch(net_connect, device_type='arista_eos')
device_type = net_connect.device_type
device_prompt = net_connect.base_prompt
print(device_type, device_prompt)

【问题讨论】:

    标签: python network-programming


    【解决方案1】:

    adding net_connect.enable() 之后,下面的东西对我有用:

    import time
    from netmiko import ConnectHandler, redispatch
    
    zpe_username = "serviceaccount"
    zpe_password = "xxxxxxx"
    zpe_hostname = "TerminalServerName"
    console_username = zpe_username + ":ttyS" + "1"
    console_server = {
        "host": zpe_hostname,
        "username": console_username,
        "password": zpe_password,
        "device_type": "terminal_server",
    }
    print("ssh " + console_username + "@" + zpe_hostname)
    
    net_connect = ConnectHandler(**console_server)
    net_connect.enable()
    net_connect.write_channel(zpe_username + "\n")
    time.sleep(1)
    password_prompt = net_connect.read_channel()
    net_connect.write_channel(zpe_password + "\n")
    time.sleep(1)
    
    redispatch(net_connect, device_type='arista_eos')
    device_type = net_connect.device_type
    device_prompt = net_connect.base_prompt
    print(device_type, device_prompt)
    

    【讨论】:

      【解决方案2】:

      您尝试过Paramiko 库吗?我通过它看到了对 HTTP 代理的支持。我之前在公司代理后面使用过它。

      This 是我所遵循的。

      【讨论】:

      • 我明天试试。不确定http代理是否有用,因为我们没有使用http,我们只是尝试通过终端服务器通过特定端口连接到设备。如问题中的屏幕截图所示。
      • 我认为标题有点误导,从您的屏幕截图中我也认为您正在尝试创建通往设备的隧道?如果是这样,请查看SSH Tunelling
      • @AvneeshSrivastava 链接已失效
      猜你喜欢
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多