0

Python は初めてですが、Python コードを使用して cisco ルーターに telnet で接続できました。画面にコマンドを表示することはできますが、出力をローカルの Linux マシン (Python スクリプトが存在する場所) に保存したいと考えています。

助言がありますか?

私の目的は、出力をローカルに保存してから、matplotlib をインポートして、帯域幅の使用状況、CPU の使用状況、メモリの使用状況、およびインターフェイスの使用状況のかなり気の利いたグラフを描画することです。

4

2 に答える 2

0

以下のスクリプトは、このスクリプトを実行しているサーバー上のファイルにデータを保存します。ファイル名は routeroutput になります。以下のコードにスイッチ IP、パスワード、有効化パスワードを入力し、Python を使用してサーバーから実行するだけです。

pexpect という追加モジュールが必要です。ここからダウンロードしてインストールできますhttps://pypi.python.org/pypi/pexpect/

import pexpect


try:
switchIP= 'x.x.x.x'
switchPassword = 'your-switch-password'
switchEnable= 'your-enable-password'
commandTorun= 'The command you want to run'


telnet = 'telnet ' + switchIP

#Login to the switch
t=pexpect.spawn(telnet)
t.expect('word:')
t.sendline(switchPassword)
t.expect('#')
t.sendline(switchEnable)
t.expect('>')

#Send the command
t.sendline('commandTorun')
t.expect('>')
data =  t.before 

#Closing the Telnet Connection
t.sendline('exit')
t.expect('>')
t.sendline('exit')
t.expect(pexpect.EOF)

#Opening the file and writing the data to it
f = open('routeroutput', 'w')
f.write(data)
f.close()

except Exception, e:
print "The Script failed to login"
print str(e)
于 2015-02-20T07:23:52.330 に答える
0

何をしようとしているかについては、telnet I/O を処理しようとする代わりに、SNMP を使用することを検討する必要があります。

説明している値を引き出して、選択したデータ ストレージ (テキスト、mysql など) に入れることができます。

http://pysnmp.sourceforge.net/

http://www.cisco.com/en/US/docs/ios/12_2/configfun/configuration/guide/fcf014.html

于 2013-09-03T05:36:18.120 に答える