python telnetlib怎么下_python3下使用telnetlib模块连接网络设备

本文介绍了在Python3环境下如何使用telnetlib模块连接并操作网络设备,包括解决字节与字符匹配问题,以及编写脚本进行批量设备交互。示例代码中展示了连接设备、输入用户名和密码、执行命令以及解析输出结果的过程。
摘要由CSDN通过智能技术生成

python3下使用telnetlib模块连接网络设备经常会遇到字节与字符不匹配的问题

问题提示如下:

import telnetlib

Host = "10.10.10.10"

# 连接Telnet服务器

tn = telnetlib.Telnet(Host, port=23, timeout=10)

tn.set_debuglevel(0)

# 输入登录用户名

tn.read_until(b'login: ')

tn.write(b"admin" + b'\n')

# 输入登录密码

tn.read_until(b'Password: ')

tn.write(b"Admin@1234" + b'\n')

tn.read_until(b'#')

tn.write(b"cd /home/sd" + b'\n')

tn.read_until(b'#')

tn.write(b"ls -al" + b'\n')

r = tn.read_until(b'#').decode('ASCII')

r1 = r.split(r"\r\n")

for i in r1:

print(i)

tn.close()

以下是设备实例:

>>> tn=telnetlib.Telnet("10.10.0.6",timeout=2)

>>> tn.read_until(b'login: ',timeout=2)

b"\r\n******************************************************************

****\r\n* Copyright (c) 2004-2018 New H3C Technologies Co., Ltd. All rig

rved.*\r\n* Without the owner's prior written consent,

*\r\n* no decompiling or reverse-engineering shall be allowed.

*\r\n**********************************************************

************\r\n\r\nlogin: "

>>> tn.write(b'admin'+b'\n')

>>> tn.read_until(b'Password: ',timeout=2)

b'jgtl\r\r\nPassword: '

>>> tn.write(b'Admin@123'+b'\n')

>>> tn.read_until(b'>')

b'\r\n'

>>> tn.write(b'ping 10.10.0.7')

>>> tn.read_until(b'>')

以上是命令行执行的过程。写成脚本需要考虑两个问题,一个是变量的替换如何编码解封,一个是输出结果加解码

#-*- coding:utf-8 -*-

import telnetlib

import re

import csv

import sys

import time

from datetime import datetime

host_dict={

"ip":"10.10.0.6",

"user":"admin",

"pwd":"Admin@123"

}

def get_loss(addrlist):

host=host_dict["ip"]

user=host_dict["user"]

pwd=host_dict["pwd"]

print (host)

resultlist = []

#try:

tn = telnetlib.Telnet(host, timeout=2)

print ("AA")

if len(host_dict["pwd"]) and len(host_dict["user"]):

print ("BB")

tn.read_until(b"login: ", timeout=3)

#tn.write(b"admin"+b"\n")

tn.write(user.encode()+b"\n")

tn.read_until(b"Password: ", timeout=3)

#tn.write(b"Admin@123"+b"\n")

tn.write(pwd.encode()+ b"\n")

# p_error = re.compile("found at")

if tn.read_until(b">", timeout=4).find(b">") != -1:

print("Connect to {host} ...... ".format(host=host))

tn.write(b"ping 127.0.0.1\n")

print (tn.read_until(b'01>'))

else:

print("%s Wrong username or password!!!" % host)

return ""

#tn.read_until(b">")

if len(addrlist) != 0:

for i in range(len(addrlist)-1):

tep = {}

command = "ping " + addrlist[i]

print("command:", command)

tn.write(command.encode() + b"\n")

result = str(tn.read_until(b"01>"))

print(result)

re_loss = re.compile("\d+\.\d+%")

loss = re_loss.findall(result)

tep[host] = loss[0]

resultlist.append(tep)

#if p_error.search(result.decode()):

# print("There is a error in this command: {0}".format(c.decode()))

tn.close()

#except Exception as e:

#if e:

# print ("Connect to {host} Failed!!!".format(host=host),e)

#return ""

return resultlist

if __name__=="__main__":

addrlist=['10.10.0.2','10.10.0.5']

print ("get_loss",get_loss(addrlist))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值