1

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?

Enno Gröper
  • 4,391
  • 1
  • 27
  • 33
D Go
  • 13
  • 4

3 Answers3

3

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.

John Jensen
  • 422
  • 4
  • 20
  • Thank you John - That's exactly what's happening here. I will be trying that shortly – D Go Dec 03 '15 at 10:34
  • John! you are the man - it worked like a charm. About 30 days back i got into JunOS and still learning my way thru it. I know for Cisco my command was terminal length 0... – D Go Dec 03 '15 at 10:44
  • Yep, and Exscript has built-in "connector" backends that should be setting `term len 0` for you automatically whenever you connect to a piece of Cisco gear. But for JunOS, I'm not sure if there's a way to disable paging at the VTY session level like there is on Cisco devices. – John Jensen Dec 03 '15 at 19:08
  • 1
    @JohnJensen, just a friendly addition... `set cli screen-length 0` is analogous to `term len 0` – Mike Pennington Dec 18 '21 at 19:41
0

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()
Vijay Shetty
  • 901
  • 10
  • 11
0

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"
           ]
}
  • This will connect using PyEZ and get the configuration with the set format and print the output, also it will get the fact(fqdn) and create the name of the router with extension .txt with the output in this case all the set commands. – Ramon Rivera Llavona Feb 01 '18 at 22:46