Support Support Downloads Knowledge Base Juniper 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



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

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.


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 address is part of mgmt security zone.

In the following example, we are connecting to the address:, 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='', user='root', password='test123
ss = StartShell(hostname)
pprint('cli -c "show version | no-more"'))       # <-- For regular CLI commands
pprint('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands

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


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

python3> 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