python 處理telnet返回的More,以及get想要的那個參數(shù)方法
更新時間:2019年02月14日 10:03:44 作者:BigDeng_2014
今天小編就為大家分享一篇python 處理telnet返回的More,以及get想要的那個參數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
問題:
需要循環(huán)獲取網(wǎng)元返回的某個參數(shù),并計算出平均值。
解決方案:
通過expect解決返回More的問題。
通過具體的參數(shù)位置,精確獲取到參數(shù)。
討論:
參數(shù)位置固定,不好復用。
#! usr/bin/env python
# -*- coding: utf-8 -*-
import telnetlib
import math
import time
def get_param(b):
"獲取相應的參數(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的所有返回結果
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遠程登錄"
# 連接Telnet服務器
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服務器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))
'''
運行結果:
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想要的那個參數(shù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python如何telnet到網(wǎng)絡設備
- 如何在Python3中使用telnetlib模塊連接網(wǎng)絡設備
- Python telnet登陸功能實現(xiàn)代碼
- 使用python telnetlib批量備份交換機配置的方法
- 對python使用telnet實現(xiàn)弱密碼登錄的方法詳解
- 使用python Telnet遠程登錄執(zhí)行程序的方法
- Python判斷telnet通不通的實例
- Python實現(xiàn)telnet服務器的方法
- Python實現(xiàn)的使用telnet登陸聊天室實例
- python實現(xiàn)telnet客戶端的方法
- Python實現(xiàn)Telnet自動連接檢測密碼的示例
相關文章
pygame面向?qū)ο蟮娘w行小鳥實現(xiàn)(Flappy bird)
這篇文章主要介紹了pygame面向?qū)ο蟮娘w行小鳥實現(xiàn)(Flappy bird),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Windows 安裝 Anaconda3+PyCharm的方法步驟
這篇文章主要介紹了Windows 安裝 Anaconda3+PyCharm的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06

