Support Support Downloads Knowledge Base Apex Support Portal Community

Knowledge Base

Search our Knowledge Base sites to find answers to your questions.

Ask All Knowledge Base Sites All Knowledge Base Sites JunosE Defect (KA)Knowledge BaseSecurity AdvisoriesTechnical BulletinsTechnotes Sign in to display secure content and recently viewed articles

[Junos] Python script for connecting to device via SSH and obtaining CLI command outputs

2

0

Article ID: KB34524 KB Last Updated: 04 Jun 2019Version: 1.0
Summary:

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

Comment on this article > Affected Products Browse the Knowledge Base for more articles related to these product categories. Select a category to begin.

Getting Up and Running with Junos

Getting Up and Running with Junos Security Alerts and Vulnerabilities Product Alerts and Software Release Notices Problem Report (PR) Search Tool EOL Notices and Bulletins JTAC User Guide Customer Care User Guide Pathfinder SRX High Availability Configurator SRX VPN Configurator Training Courses and Videos End User Licence Agreement Global Search