本文实例讲述了Python实现数通设备端口使用情况监控的方法。分享给大家供大家参考。具体如下:
最近因工作需要,上面要求,每天需上报运维的几百数通设备端口使用情况【】,虽然有现成网管监控工具监控设备状态,但做报表,有点不方便,特写了个小脚本。上传的,为半成品可用程序
注:测试运行于ubuntn,需安装snmpwalk工具,
目标数通设备,需做好相关snmp配置
#/usr/bin/python #coding:utf-8 import os,sys import re from pprint import pprint #甯歌鍘傚鏁伴?氳澶嘙IB鍊? MIB = { 'public':{ 'ports':{ 'string':'.1.3.6.1.2.1.2.2.1.2', 'status':'.1.3.6.1.2.1.2.2.1.8', #2 down } }, 'huawei':{ }, 'cisco':{ } } #绔彛鐘舵?佽浆鎹? def portStatus(_s): if int(_s)==2: return 'down' elif int(_s)==1: return 'up' else: return 'none' #鎵ц绯荤粺鍛戒护snmpwalk璋冪敤 def snmpwalk(host,publicKey,iso): return [i.strip() for i in os.popen('/usr/bin/snmpwalk -c %s -v 2c %s %s'%(publicKey,host,iso)).readlines()] def getPorts(_ip,_public,option): if option == 'ports': postsString = (j.split('=')[1].split(':')[1].replace('"','').strip() for j in snmpwalk(_ip,_public,MIB['public']['ports']['string'])) postsStatus = (int(j.split('=')[1].split(':')[1].strip()) for j in snmpwalk(_ip,_public,MIB['public']['ports']['status'])) return zip(postsString,postsStatus) else: print('on this commmnad') publicKey = 'hi' #sunmp public key HOSTS={ '10.221.98.2':{'type':'switch','origin':'quidway','public':publicKey}, '10.221.98.3':{'type':'switch','origin':'quidway','public':publicKey}, '10.221.97.108':{'type':'firewall','origin':'h3c','public':publicKey}, '10.231.98.233':{'type':'switch','origin':'cisco','public':publicKey}, } if __name__ == '__main__': for i in HOSTS.keys(): for host,status in getPorts(i,HOSTS[i]['public'],'ports'): print("%s\t%s\t%s"%(i,host.ljust(30),portStatus(status).ljust(20))) print(''.ljust(50,'#'))
运行结果:
root@ubuntn12:/tmp# python snmpwalk.py
10.221.98.2 InLoopBack0 up
10.221.98.2 NULL0 up
10.221.98.2 GigabitEthernet0/0/0 down
10.221.98.2 Aux0/0/1 down
10.221.98.2 Vlanif100 up
10.221.98.2 Eth-Trunk1 down
10.221.98.2 Eth-Trunk1.32 down
10.221.98.2 Eth-Trunk1.50 down
10.221.98.2 Eth-Trunk1.60 down
10.221.98.2 Eth-Trunk1.70 down
10.221.98.2 Eth-Trunk1.80 down
10.221.98.2 Eth-Trunk1.100 down
10.221.98.2 Eth-Trunk1.110 down
.......
希望本文所述对大家的Python程序设计有所帮助。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ホットトピック









