Python Telnet设备自动化
我尝试使用telnet协议用python连接设备,以实现自动化(例如一些旧的Cisco路由器), 为此,我使用的是凝固汽油弹库(基于凝固汽油弹库,基于telnetlib库) 问题是,当我直接使用telnetlib库时,它工作正常,但当我使用凝固汽油弹或 Netmiko它给出了以下错误:get Telnet登录失败 你以前有过这种情况吗 PS:我尝试了一些在互联网上找到的解决方案,但没有任何效果 先谢谢你 此代码有效(telnetlib库): 此代码返回登录错误(凝固汽油弹库): 此代码返回登录错误(netmiko库):Python Telnet设备自动化,python,telnet,telnetlib,netmiko,napalm,Python,Telnet,Telnetlib,Netmiko,Napalm,我尝试使用telnet协议用python连接设备,以实现自动化(例如一些旧的Cisco路由器), 为此,我使用的是凝固汽油弹库(基于凝固汽油弹库,基于telnetlib库) 问题是,当我直接使用telnetlib库时,它工作正常,但当我使用凝固汽油弹或 Netmiko它给出了以下错误:get Telnet登录失败 你以前有过这种情况吗 PS:我尝试了一些在互联网上找到的解决方案,但没有任何效果 先谢谢你 此代码有效(telnetlib库): 此代码返回登录错误(凝固汽油弹库): 此代码返回登录错
欢迎来到StackOverflow!为了让我们更好地帮助您,请您更新您的问题,使其以一种新的方式显示您的相关代码。如果您能让我们知道您迄今为止为解决您的问题所做的努力,这也会很有帮助。有关更多信息,请参阅此类库通常提供查看通过适当配置发送和接收的实际字节的方法。您可能需要在文档中搜索此项。
import telnetlib
import time
from pprint import pprint
def to_bytes(line):
return f"{line}\n".encode("utf-8")
def send_show_command(ip, username, password, enable, commands):
with telnetlib.Telnet(ip) as telnet:
telnet.read_until(b"Username")
telnet.write(to_bytes(username))
telnet.read_until(b"Password")
telnet.write(to_bytes(password))
index, m, output = telnet.expect([b">", b"#"])
if index == 0:
telnet.write(b"enable\n")
telnet.read_until(b"Password")
telnet.write(to_bytes(enable))
telnet.read_until(b"#", timeout=5)
telnet.write(b"terminal length 0\n")
telnet.read_until(b"#", timeout=5)
time.sleep(3)
telnet.read_very_eager()
result = {}
for command in commands:
telnet.write(to_bytes(command))
output = telnet.read_until(b"#", timeout=5).decode("utf-8")
result[command] = output.replace("\r\n", "\n")
return result
if __name__ == "__main__":
devices = ["1.1.1.1"]
commands = ["sh ip int br"]
for ip in devices:
result = send_show_command(ip, "username", "password", "", commands)
pprint(result, width=120)
from napalm import get_network_driver
from pprint import pprint
driver = get_network_driver('ios')
conn_method = {'port': 23, 'transport': 'telnet', 'global_delay_factor': 2, 'secret': ''}
host = '1.1.1.1'
user = 'username'
passwd = 'password'
with driver(hostname=host, username=user, password=passwd, optional_args=conn_method ) as device:
print('Getting facts')
pprint(device.get_facts())
import os
from netmiko import ConnectHandler
switch = {
'device_type': 'cisco_ios_telnet',
'ip': '1.1.1.1',
"username": "username",
"password": "password",
"timeout": 15
}
net_connect = ConnectHandler(**switch)
print(net_connect)