如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
Python中專門提供了telnetlib庫,用來完成基于telnet協(xié)議的通信功能。
python3下使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備經(jīng)常會(huì)遇到字節(jié)與字符不匹配的問題
問題提示如下:
import telnetlib
Host = "10.10.10.10"
# 連接Telnet服務(wù)器
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()
以下是設(shè)備實(shí)例:
>>> 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<bangong-01>'
>>> tn.write(b'ping 10.10.0.7')
>>> tn.read_until(b'>')
以上是命令行執(zhí)行的過程。寫成腳本需要考慮兩個(gè)問題,一個(gè)是變量的替換如何編碼解封,一個(gè)是輸出結(jié)果加解碼
#-*- 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))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在python win系統(tǒng)下 打開TXT文件的實(shí)例
下面小編就為大家分享一篇在python win系統(tǒng)下 打開TXT文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python concurrent.futures模塊使用實(shí)例
這篇文章主要介紹了Python concurrent.futures模塊使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
用Python獲取智慧校園每日課表并自動(dòng)發(fā)送至郵箱
很多小伙伴們都在為查看智慧校園課表而煩惱,今天特地整理了這篇文章,不僅可以用Python獲取智慧校園每日課表,還會(huì)自動(dòng)發(fā)至你郵箱,需要的朋友可以參考下2021-05-05
Python subprocess模塊功能與常見用法實(shí)例詳解
這篇文章主要介紹了Python subprocess模塊功能與常見用法,結(jié)合實(shí)例形式詳細(xì)分析了subprocess模塊功能、常用函數(shù)相關(guān)使用技巧,需要的朋友可以參考下2018-06-06
Python+Plotly繪制精美的數(shù)據(jù)分析圖
Plotly?是目前已知的Python最強(qiáng)繪圖庫,比Echarts還強(qiáng)大許多。它的繪制通過生成一個(gè)web頁面完成,并且支持調(diào)整圖像大小,動(dòng)態(tài)調(diào)節(jié)參數(shù)。本文將利用Plotly繪制精美的數(shù)據(jù)分析圖,感興趣的可以了解一下2022-05-05
詳解使用python的logging模塊在stdout輸出的兩種方法
這篇文章主要介紹了詳解使用python的logging模塊在stdout輸出的相關(guān)資料,需要的朋友可以參考下2017-05-05
Python實(shí)現(xiàn)Excel和CSV之間的相互轉(zhuǎn)換
通過使用Python編程語言,編寫腳本來自動(dòng)化Excel和CSV之間的轉(zhuǎn)換過程,可以批量處理大量文件,定期更新數(shù)據(jù),并集成轉(zhuǎn)換過程到自動(dòng)化工作流程中,本文將介紹如何使用Python 實(shí)現(xiàn)Excel和CSV之間的相互轉(zhuǎn)換,需要的朋友可以參考下2024-03-03
使用jupyter notebook運(yùn)行python和R的步驟
這篇文章主要介紹了使用jupyter notebook運(yùn)行python和R的步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

