Python Exscript - JunOS

a 夏天 提交于 2019-12-11 13:58:58

问题


I'm trying to to run a script to show all of the configuration and write them on files for juniper and CISCO routers. So far the CISCO script is working as it should but the thing is with the juniper router.

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

After getting the list of devices to query, I run this but it gets stuck as if there is a limit of buffer... -

If I attempt to do a different command:
("show configuration | display set | match destination ")
-- I get the output written on a file or screen.

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

=========== ==== Question - How do i get the script to RUN AND PROVIDE THE OUTPUT OF the command: show configuration | display set the 2nd pic shows the error i get but if I change the command to: show configuration | display set | match description I get the information requested. Am i missing to add something in the module so that exscript/python to avoid the timeout?


回答1:


By default, JunOS paginates lengthy output returned by any command. What's likely happening is that the Juniper device that you're connecting to is paginating the output of the show configuration | display set command, and Exscript is timing out because the device is waiting on user input to continue pagination of the output of the command, rather than returning a prompt that Exscript recognizes.

I would make the following modification:

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

This will disable output pagination for that particular command, and should return immediately to the prompt and allow Exscript to return the output to you. For good measure I also add a carriage return to my commands as well, ie:

cmd2 = 'show configuration | display set | no-more\r'

But the usefulness of doing the above is debatable, as if I remember correctly the execute() method should be doing this for you anyway.




回答2:


For handling Junos devices using python, I would recommend you to use 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:


I use this script using PyEZ with JSON for using multiples IP addresses.

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

the JSON file looks like:

   {
  "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"
           ]
}


来源:https://stackoverflow.com/questions/34042841/python-exscript-junos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!