python 處理telnet返回的More,以及get想要的那個(gè)參數(shù)方法
問題:
需要循環(huán)獲取網(wǎng)元返回的某個(gè)參數(shù),并計(jì)算出平均值。
解決方案:
通過expect解決返回More的問題。
通過具體的參數(shù)位置,精確獲取到參數(shù)。
討論:
參數(shù)位置固定,不好復(fù)用。
#! usr/bin/env python # -*- coding: utf-8 -*- import telnetlib import math import time def get_param(b): "獲取相應(yīng)的參數(shù),返回float型參數(shù)組" c = [] b = list(b) length = len(b) print length for x in b: c.append(float(x)) print c return c def get_avg(a): "獲取平均值" length = len(a) sum = 0 for x in a: sum += x avg = sum/length return avg def get_telnet(tn): "獲取telnet數(shù)據(jù)" for command in commands: tn.write('%s\n' % command) time.sleep(0.5) ## result = tn.read_very_eager() # 不用read_all(),不能處理More print "**************" a = [] a.append('More') print a result = str() while True: b,c,d = tn.expect(a,timeout=1) print b # 有More為0,無More為-1 print 'cccccccccccccccccccccccccccccccccccccccccccc' print c print 'dddddddddddddddddddddddddddddddddddddddddddd' print d result += d if 0 == b: print "There has 'More'!!!" tn.write(r' ') #不用\r\n來繼續(xù) else: break print 'get result success!' print result #獲取到帶More的所有返回結(jié)果 a = result.split('\r\n') # 不要加r length = len(a) print length b = a[1].split(' ') print b print a[32] c = a[32] d = c.split(' ') print d length = len(d) print d[8] e = d[8].split('(') print e[0] return e[0] def close_telnet(tn): "執(zhí)行完畢后,終止Telnet連接(或輸入exit退出)" tn.write('exit\n') tn.close() def open_telnet(Host, username, password, finish, commands): "Telnet遠(yuǎn)程登錄" # 連接Telnet服務(wù)器 tn = telnetlib.Telnet(Host, port=23, timeout=10) tn.set_debuglevel(2) # 輸入登錄用戶名 tn.read_until('Username:') tn.write(username + '\n') # 輸入登錄密碼 tn.read_until('Password:') tn.write(password + '\n') # 登錄完畢后執(zhí)行命令 tn.read_until(finish) return tn if __name__=='__main__': Host = '' # Telnet服務(wù)器IP username = '' # 登錄用戶名 password = '' # 登錄密碼 finish = '#' # 命令提示符 param = [] commands = ['sho optical-module-info xgei-1/3/1'] tn = open_telnet(Host, username, password, finish, commands) for i in range(1,10): param.append(get_telnet(tn)) close_telnet(tn) print param print get_avg(get_param(param)) ''' 運(yùn)行結(jié)果: 37 ['Optical', 'Module', 'Position', ':', 'xgei-1/3/1'] Bias-Upper : 131(mA) Bias-Lower : 0(mA) ['Bias-Upper', '', '', '', '', '', '', ':', '131(mA)', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Bias-Lower', '', '', '', '', '', '', ':', '0(mA)'] 131(mA) 131 Telnet(172.10.1.123,23): send 'exit\n' ['131', '131', '131', '131', '131', '131', '131', '131', '131'] 9 [131.0, 131.0, 131.0, 131.0, 131.0, 131.0, 131.0, 131.0, 131.0] 131.0 >>> '''
以上這篇python 處理telnet返回的More,以及get想要的那個(gè)參數(shù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python如何telnet到網(wǎng)絡(luò)設(shè)備
- 如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
- Python telnet登陸功能實(shí)現(xiàn)代碼
- 使用python telnetlib批量備份交換機(jī)配置的方法
- 對python使用telnet實(shí)現(xiàn)弱密碼登錄的方法詳解
- 使用python Telnet遠(yuǎn)程登錄執(zhí)行程序的方法
- Python判斷telnet通不通的實(shí)例
- Python實(shí)現(xiàn)telnet服務(wù)器的方法
- Python實(shí)現(xiàn)的使用telnet登陸聊天室實(shí)例
- python實(shí)現(xiàn)telnet客戶端的方法
- Python實(shí)現(xiàn)Telnet自動(dòng)連接檢測密碼的示例
相關(guān)文章
pygame面向?qū)ο蟮娘w行小鳥實(shí)現(xiàn)(Flappy bird)
這篇文章主要介紹了pygame面向?qū)ο蟮娘w行小鳥實(shí)現(xiàn)(Flappy bird),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Windows 安裝 Anaconda3+PyCharm的方法步驟
這篇文章主要介紹了Windows 安裝 Anaconda3+PyCharm的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06python 二維數(shù)組90度旋轉(zhuǎn)的方法
今天小編就為大家分享一篇python 二維數(shù)組90度旋轉(zhuǎn)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python3實(shí)現(xiàn)爬取淘寶美食代碼分享
本文給大家分享的是如何使用python3來爬取淘寶美食圖片標(biāo)題等信息的方法和代碼,有需要的小伙伴可以參考下2018-09-09