Python通过终端使用密码连接到网络设备

2024-05-31 18:43:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

其中用户名是用户名,后面的号码:是我们要连接的端口号,终端服务器名是代理服务器的名称。我已经尝试了从这两个链接Link1和{a2}得到的代码

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那样通过代理/终端服务器连接到网络设备并获得设备的主机名吗。在

Added sample screen shot of Putty, which I want simulate using Python

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

^{pr2}$

Tags: 代码fromimport密码代理nettimeconnect
2条回答

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)

你试过Paramiko库吗?我通过它看到了对HTTP代理的支持。我不久前在公司代理的时候用过它。在

This是我一直遵循的。在

相关问题 更多 >