python 處理telnet返回的More,以及get想要的那個(gè)參數(shù)方法
問(wèn)題:
需要循環(huán)獲取網(wǎng)元返回的某個(gè)參數(shù),并計(jì)算出平均值。
解決方案:
通過(guò)expect解決返回More的問(wèn)題。
通過(guò)具體的參數(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,無(wú)More為-1
print 'cccccccccccccccccccccccccccccccccccccccccccc'
print c
print 'dddddddddddddddddddddddddddddddddddddddddddd'
print d
result += d
if 0 == b:
print "There has 'More'!!!"
tn.write(r' ') #不用\r\n來(lái)繼續(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ù)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python如何telnet到網(wǎng)絡(luò)設(shè)備
- 如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
- Python telnet登陸功能實(shí)現(xiàn)代碼
- 使用python telnetlib批量備份交換機(jī)配置的方法
- 對(duì)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)連接檢測(cè)密碼的示例
相關(guān)文章
如何用python識(shí)別滑塊驗(yàn)證碼中的缺口
這篇文章主要介紹了如何用python識(shí)別滑塊中的缺口,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python Jupyter運(yùn)行時(shí)間實(shí)例過(guò)程解析
這篇文章主要介紹了python Jupyter運(yùn)行時(shí)間實(shí)例過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
python實(shí)現(xiàn)人臉簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)人臉簽到系統(tǒng),帶數(shù)據(jù)庫(kù)存儲(chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Pytorch使用CUDA流(CUDA?stream)的實(shí)現(xiàn)
本文主要介紹了Pytorch使用CUDA流(CUDA?stream)的實(shí)現(xiàn),CUDA流是在GPU上并行執(zhí)行操作的一種機(jī)制,通過(guò)使用CUDA流,可以將不同的操作分配給不同的流,在不同的流上并行執(zhí)行這些操作,從而提高代碼的性能2023-12-12
Python列表list數(shù)組array用法實(shí)例解析
這篇文章主要介紹了Python列表list數(shù)組array用法,實(shí)例解析了關(guān)于數(shù)組的各種常見操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
Python?ChineseCalendar包主要類和方法詳解
ChineseCalendar?是一個(gè)?Python?包,用于獲取中國(guó)傳統(tǒng)日歷信息。這個(gè)包提供了中國(guó)農(nóng)歷、二十四節(jié)氣、傳統(tǒng)節(jié)日、黃歷等信息,這篇文章主要介紹了Python?ChineseCalendar包簡(jiǎn)介,需要的朋友可以參考下2023-03-03
使用python請(qǐng)求接口方式(可進(jìn)行并發(fā)測(cè)試)
這篇文章主要介紹了使用python請(qǐng)求接口方式(可進(jìn)行并發(fā)測(cè)試),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

