如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
Python中專門提供了telnetlib庫(kù),用來(lái)完成基于telnet協(xié)議的通信功能。
python3下使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備經(jīng)常會(huì)遇到字節(jié)與字符不匹配的問(wèn)題
問(wèn)題提示如下:
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í)行的過(guò)程。寫(xiě)成腳本需要考慮兩個(gè)問(wèn)題,一個(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實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷例子分享
這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷例子分享,需要的朋友可以參考下2014-04-04Python將多個(gè)excel表格合并為一個(gè)表格
這篇文章主要為大家詳細(xì)介紹了Python將多個(gè)excel表格合并為一個(gè)表格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Python如何爬取實(shí)時(shí)變化的WebSocket數(shù)據(jù)的方法
這篇文章主要介紹了Python如何爬取實(shí)時(shí)變化的WebSocket數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Python用正則表達(dá)式實(shí)現(xiàn)爬取古詩(shī)文網(wǎng)站信息
這篇文章主要給大家介紹了關(guān)于Python如何利用正則表達(dá)式爬取爬取古詩(shī)文網(wǎng)站信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Python3.2中Print函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python3.2中Print函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python3.2中Print函數(shù)輸出的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05使用python讀取CSV文件時(shí)遇到編碼問(wèn)題解決方案
這篇文章主要介紹了用python讀取CSV文件時(shí)遇到編碼問(wèn)題,本文給大家分享最優(yōu)解決方案,通過(guò)使用csvkit,它使用自動(dòng)檢測(cè)適當(dāng)?shù)木幋a和解碼,需要的朋友可以參考下2023-08-08