2015-12-02 28 views
1

我試圖運行腳本來顯示所有配置,並將它們寫入juniper和CISCO路由器的文件中。 到目前爲止,CISCO腳本的工作原理與Juniper路由器一樣。Python Exscript - JunOS

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:\User\suserrr\Downloads\shrun.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:\Users\userrr\Downloads\shrun.py", line 40, in <module> 
    conn.execute(cmd2) 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 900, in execute 
    return self.expect_prompt() 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 999, in expect_prompt 
    result = self.expect(self.get_prompt()) 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 980, in expect 
    result = self._expect(prompt) 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 956, in _expect 
    result = self._domatch(to_regexs(prompt), True) 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\SSH2.py", line 329, in _domatch 
    if not self._fill_buffer(): 
    File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\SSH2.py", line 303, in _fill_buffer 
    raise TimeoutException(error) 
Exscript.protocols.Exception.TimeoutException: Timeout while waiting for response from device 

=========== ====問題 - 如何讓腳本運行,並提供命令的輸出:show configuration | display set第二PIC顯示錯誤我得到但是如果我將命令更改爲:show configuration | display set | match description,我會收到請求的信息。我是否缺少在模塊中添加一些內容,以便exscript/python避免超時?

回答

1

默認情況下,JunOS爲任何命令返回的冗長輸出分頁。可能發生的情況是,您要連接到的Juniper設備正在對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-more\r' 

但上面做的用處是值得商榷的,因爲如果我沒有記錯的​​方法應該爲你反正做這個。

+0

謝謝約翰 - 這正是這裏發生了什麼。我將很快嘗試 –

+0

約翰!你是男人 - 它像魅力一樣工作。大約30天后,我進入了JunOS,仍然在學習通過它的方式。我知道思科我的命令終端長度爲0 ... –

+0

是的,Exscript有內置的「連接器」後端,當你連接到一個思科設備時,應該自動爲你設置「term len 0」。但對於JunOS,我不確定是否有辦法在VTY會話級別禁用分頁,就像在思科設備上一樣。 –

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() 
0

我用用PyEZ這個腳本JSON使用倍數的IP地址。

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" 
      ] 
} 
+0

這將使用PyEZ進行連接,並獲得具有設置格式的配置並打印輸出,同時它將獲得事實(fqdn)並創建具有擴展名.txt的路由器的名稱,在這種情況下輸出爲所有set命令。 –

相關問題