国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 處理telnet返回的More,以及get想要的那個參數方法

2020-02-16 01:08:37
字體:
來源:轉載
供稿:網友

問題:

需要循環獲取網元返回的某個參數,并計算出平均值。

解決方案:

通過expect解決返回More的問題。

通過具體的參數位置,精確獲取到參數。

討論:

參數位置固定,不好復用。

#! usr/bin/env python# -*- coding: utf-8 -*-import telnetlibimport mathimport timedef get_param(b): "獲取相應的參數,返回float型參數組" c = [] b = list(b) length = len(b) print length for x in b: c.append(float(x)) print c return cdef get_avg(a): "獲取平均值" length = len(a) sum = 0 for x in a: sum += x avg = sum/length return avgdef get_telnet(tn): "獲取telnet數據" 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來繼續  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): "執行完畢后,終止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') # 登錄完畢后執行命令  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)131Telnet(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想要的那個參數方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙州县| 六安市| 易门县| 雷山县| 资中县| 玉树县| 洞头县| 永康市| 静海县| 独山县| 东莞市| 竹北市| 武义县| 乌兰浩特市| 云霄县| 白山市| 靖宇县| 高陵县| 九龙县| 柳河县| 什邡市| 内黄县| 琼中| 沂南县| 白城市| 巢湖市| 公安县| 炉霍县| 丰都县| 收藏| 九台市| 娱乐| 临潭县| 上思县| 兰溪市| 林甸县| 罗平县| 九龙县| 陆河县| 碌曲县| 汉源县|