pythonciscojuniper-network-connectexscript

Python Exscript - JunOS


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?


Solution

  • 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.