Description

This article provides the basic script for connecting to the Juniper Routers via SSH, authenticate via Root user, and to get the CLI/VTY command outputs from any remote server.

Solution

Minimum configuration on Junos devices (MX/PTX/QFX devices) for the script to run:

set system services netconf ssh​
set  system services ssh root-login allow​


For SRX, the following is also needed:

set security zones security-zone mgmt host-inbound-traffic system-services ssh


Note that interface with 10.1.1.1 address is part of mgmt security zone.

In the following example, we are connecting to the address: 10.1.1.1, with the user root and Password: test123​ using Python Script:

from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
hostname= Device(host=' 10.1.1.1 ', user=' root ', password=' test123
' )
ss = StartShell(hostname)
ss.open()
hostname.open()
pprint(ss.run('cli -c "show version | no-more"'))       # <-- For regular CLI commands
pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
hostname.close()
ss.close()

Save the above contents as ".py" file and run it using the following command:

python3 filename.py

You can also save the outputs to a file instead of printing them on the console:

python3 filename.py> save logfile.txt