/ / Python Exscript - JunOS - python、cisco、juniper-network-connect、exscript

Python Exscript - JunOS - python、cisco、juniper-network-connect、exscript

私はスクリプトを実行してjuniperおよびCISCOルーター用のファイルに書き込んでください。 これまでのCISCOのスクリプトはうまくいくはずですが、問題はジュニパーのルータにあります。

for ii in JUNIPER:
print ii
cmd2 = "show configuration | display set"
conn.connect(ii)
conn.login(account1)
conn.execute(cmd2)
print conn.response
#filerouter = open(ii, "w")
#filerouter.write(conn.response)
#filerouter.close()

クエリするデバイスのリストを取得した後、私はこれを実行するが、バッファの限界があるかのように立ち往生する... -

私が別のコマンドを実行しようとすると:
("show configuration | display set | match destination ")
- 出力をファイルまたはスクリーンに書き出します。

C:Python27>python.exe C:UsersuserrrDownloadsshrun.py
"clear" is not recognized as an internal or external command,
operable program or batch file.
Generating configs for ROUTER:  R1.test.site
Generating connect for ROUTER:  R2.test.site
==============
===========
routername
Traceback (most recent call last):
File "C:UsersuserrrDownloadsshrun.py", line 40, in <module>
conn.execute(cmd2)
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsProtocol.py", line 900, in execute
return self.expect_prompt()
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsProtocol.py", line 999, in expect_prompt
result = self.expect(self.get_prompt())
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsProtocol.py", line 980, in expect
result = self._expect(prompt)
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsProtocol.py", line 956, in _expect
result = self._domatch(to_regexs(prompt), True)
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsSSH2.py", line 329, in _domatch
if not self._fill_buffer():
File "C:Python27libsite-packagesexscript-2.1.440-py2.7.eggExscriptprotocolsSSH2.py", line 303, in _fill_buffer
raise TimeoutException(error)
Exscript.protocols.Exception.TimeoutException: Timeout while waiting for response from device

=========== ====質問 - スクリプトを実行してコマンドの出力を提供するにはどうすればいいですか? show configuration | display set 2番目の画像は私が得るエラーを示していますが、私はコマンドを変更する場合: show configuration | display set | match description 私は要求された情報を得る。 exscript / pythonがタイムアウトを避けるように、モジュールに何かを追加するのに欠けていますか?

回答:

回答№1は1

デフォルトでは、JunOSはすべてのコマンドによって返される長い出力のページを設定します。起こりそうなことは、あなたが接続しているジュニパーのデバイスが、 show configuration | display set Exscriptが認識するプロンプトを返すのではなく、コマンドの出力のページ付けを続けるためにデバイスがユーザー入力を待機しているため、Exscriptがタイムアウトしています。

私は次のように変更します:

for ii in JUNIPER:
print ii
cmd2 = "show configuration | display set | no-more"
conn.connect(ii)
conn.login(account1)
conn.execute(cmd2)
print conn.response

これにより、出力ページ設定が無効になりますプロンプトにすぐに戻り、Exscriptが出力をあなたに返すようにする必要があります。良い尺度のために、私はまた私のコマンドにキャリッジリターンを追加します。

cmd2 = "show configuration | display set | no-morer"

しかし、上記を実行することの有用性は議論の余地があります。 execute() とにかくあなたのためにこれをやっているはずです。


回答№2の場合は0

Pythonを使用してJunosデバイスを処理する場合は、PyEZを使用することをお勧めします。 https://github.com/Juniper/py-junos-eznc

from jnpr.junos import Device
from lxml import etree

dev = Device("hostname", user="username", password="Password123")
dev.open()

cnf = dev.rpc.get_config()    # similar to "show configuration | no-more" on cli
print (etree.tounicode(cnf))

dev.close()

回答№3の場合は0

私は複数のIPアドレスを使用するためにJSONでPyEZを使用してこのスクリプトを使用します。

from jnpr.junos import Device
from lxml import etree
import json


config_file = open("config.json")
config = json.load(config_file)
config_file.close()


for host in config["ip"]:

dev = Device(host=host, user=config["username"],
password=config["password"], port=22)
dev.open()
data = dev.rpc.get_config(options={"format":"set"})
file_name = dev.facts["fqdn"]
print(etree.tostring(data))
dev.close()

f = open(file_name + ".txt", "w")
f.write(etree.tostring(data))
f.close()

JSONファイルは次のようになります。

   {
"username": "user",
"password": "password",
"ip": [
"10.255.6.100",
"10.255.6.101",
"10.255.6.102",
"10.255.6.103",
"10.255.6.104"
]
}