ホームページ  >  記事  >  バックエンド開発  >  Python を使用して Cisco デバイスを管理する

Python を使用して Cisco デバイスを管理する

高洛峰
高洛峰オリジナル
2016-10-18 10:47:462419ブラウズ

tratto という、外国人が Python を使って書いた、シスコの機器を管理するための小さなフレームワークを見つけました。これを使用すると、コマンドをバッチで実行できます。

ダウンロード後は主に 3 つのファイルがあります:

Systems.py は、いくつかの異なるデバイスのオペレーティング システムとそれらの共通コマンドを定義します。

Connectivity.py は主に関数を実装するコードです。実際には、主に Python の pexpect モジュールを使用します。

Driver.py はサンプル ファイルです。

[root@safe tratto-master]# cat driver.py

#!/usr/bin/env python
import Connectivity
import Systems
#telnet to a cisco switch
m = Systems.OperatingSystems['IOS']
s = Connectivity.Session("192.168.1.1",23,"telnet",m)
s.login("yourusername", "yourpassword")
# if your need to issue an "enable" command
s.escalateprivileges('yourenablepassword')
s.sendcommand("show clock")
s.sendcommand("show run")
s.logout()

上記はサンプル driver.py の内容で、使い方はとても簡単です。

最初にデバイスのシステム バージョンを選択します。この場合は cisco スイッチなので、IOS が使用されます。著者が現在作成しているサポート可能なデバイス システムは次のとおりです。

OperatingSystems = {

'IOS': CiscoIOS,

'WebNS': CiscoWebNS,

'OSX': AppleOSX,

'SOS': SecureComputingSidewinder ,

'AOS': ArubaOS,

'OBSD': OpenBSD,

}

次に、IP、ポート、Telnet または SSH を入力し、最後に前の手順で選択したシステム バージョンを入力します。ログイン ログイン資格情報を入力します。

s.escalateprivileges は特権資格情報です。とても簡単です~

以下は、スイッチに関する情報を取得してファイルに保存するために書いたスクリプトです。

[root@safe tratto-master]#猫cisco.py

#!/usr/bin/env python
#
# Cisco Switch commands
# By s7eph4ni3
#
import Connectivity
import Systems
m = Systems.OperatingSystems['IOS']
iplist = ['192.168.1.1','192.168.1.2']
cmdlist = ['show ip int brief','show cdp nei detail','show arp','show ver']
for ip in iplist:
    if ip == '192.168.1.1':
        s = Connectivity.Session(ip,23,"telnet",m)
        s.login("", "passwd")
    else:
        s = Connectivity.Session(ip,22,"ssh",m)
        s.login("username", "passwd")
    s.escalateprivileges('enpasswd')
    f = open(ip+'.txt','w+')
    for cmd in cmdlist:
        a = s.sendcommand(cmd)
        f.write(ip+cmd+'\n')
        f.write(a+'\n')
    f.close()
    s.logout()


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。