Use Junos PyEZ to Access the Shell on Junos Devices
SUMMARY Use Junos PyEZ to connect to the shell on Junos devices and execute commands.
StartShell Overview
The Junos CLI has many operational mode commands that return information that is similar to the information returned by many shell commands. Thus, access to the UNIX-level shell on Junos devices usually is not required. However in some cases, a user or application might need to access the shell and execute shell commands or execute CLI commands from the shell.
The Junos PyEZ
jnpr.junos.utils.start_shell
module defines the
StartShell
class, which enables Junos PyEZ applications to initiate an SSH connection
to a Junos device and access the shell. The StartShell
methods
enable the application to then execute commands over the connection and retrieve the
response.
The StartShell
open()
and close()
methods establish and terminate
an SSH connection with the device. As a result, if the client application requires
access to just the shell, it can omit the calls to the Device
open()
and close()
methods.
In Junos PyEZ Release 2.6.7 and earlier, the StartShell
instance
connects to the default SSH port 22. Starting in Junos PyEZ Release 2.6.8, the
StartShell
instance connects to the same port that is defined
in the Device
instance, except in the following cases, where the
shell connection still uses port 22:
-
Device
host
is set to 'localhost' -
Device
port
is set to 830. -
Device
port
is undefined.
Execute Commands from the Shell
The StartShell
run()
method executes a shell command and waits for the response. By default,
the method waits for one of the default shell prompts (%, #, >, or $) before
returning the command output. Alternatively, you can set the
this="string"
argument to a specific
string, and the method waits for the expected string or pattern before returning the
command output.
The return value is a tuple. The first item is True
if the exit code
is 0, and False
otherwise. The second item is the output of the
command.
The following example connects to a host and executes two operational mode commands
from the shell. The script first executes the request support
information
command and saves the output to a file. The script then
executes the show version
command, stores the output in the
version
variable, and then prints the contents of the
variable.
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell dev = Device(host='router1.example.net') ss = StartShell(dev) ss.open() ss.run('cli -c "request support information | save /var/tmp/information.txt"') version = ss.run('cli -c "show version"') print (version) ss.close()
The returned tuple includes the Boolean corresponding to the exit code for the
command and the command output for the show version
command. The
output in this example is truncated for brevity.
(False, '\r \rHostname: router1\r\nModel: mx104\r\nJunos: 17.1R8\r\nJUNOS Base OS boot [17.1R1.8]\r\n ...)
Instances of the StartShell
class can also be used as context
managers. In this case, you do not need to explicitly call the
StartShell
open()
and close()
methods. For example:
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell dev = Device(host='router1.example.net') with StartShell(dev) as ss: ss.run('cli -c "request support information | save /var/tmp/information.txt"') version = ss.run('cli -c "show version"') print (version)
How to Specify the Shell Type
Starting in Junos PyEZ Release 2.6.4, StartShell
supports the
shell_type
argument within remote scripts to specify the shell
type. StartShell
supports the following shell types:
-
C Shell (csh)
-
Bourne-style shell (ash)
By default, StartShell
instances are type C Shell (csh). You can
also specify shell_type="sh"
to start a Bourne-style shell (ash).
For example:
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell dev = Device(host='router1.example.net') with StartShell(dev, shell_type="sh") as ss: version = ss.run('cli -c "show version"') print (version)
How to Specify a Timeout
You can include the StartShell
timeout
argument to specify the duration of time in seconds that
the utility must wait for the expected string or pattern before timing out. If you
do not specify a timeout, the default is 30 seconds.
The expected string is the value defined in the this
argument. If
you do not define this
, the expected string is one of the default
shell prompts. If you instead set the special value this=None
, the
device waits for the duration of the timeout before capturing the command output, as
described in Execute Nonreturning Shell Commands.
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell dev = Device(host='router1.example.net') with StartShell(dev) as ss: ss.run('cli -c "request support information | save /var/tmp/information.txt"', timeout=60) version = ss.run('cli -c "show version"') print (version)
How to Stagger Command Execution
At times, you might want to execute or loop multiple calls to the
run()
method. To help stabilize the output, you can specify the
sleep
argument. The sleep
argument instructs
the device to wait for the specified number of seconds before receiving data from
the buffer. You can define sleep
as a floating point number for
sub-second precision.
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell dev = Device(host='router1.example.net') tables = ['inet.0', 'inet.6'] with StartShell(dev) as ss: for table in tables: command = 'cli -c "show route table ' + table + '"' rsp = ss.run(command, sleep=5) pprint (rsp)
Execute Nonreturning Shell Commands
In certain cases, you might need to execute nonreturning shell commands, such as the
monitor traffic
command, which displays traffic that originates
or terminates on the local Routing Engine. In the Junos CLI, the monitor
traffic
command displays the information in real time until the user
sends a Ctrl+c keyboard sequence to stop the packet capture.
You can execute nonreturning shell commands using the StartShell
run()
method by including the argument this=None
.
When you include the this=None
argument, the method waits until the
specified timeout value to retrieve and return all command output from the shell. In
this case, the first item of the returned tuple is True
when the
result of the executed shell command returns content, and the second item is the
command output. If you omit the this
argument or set it equal to a
specific string or pattern, the method might return partial output for a
nonreturning command if it encounters a default prompt or the specified string
pattern within the command output.
The following sample code executes the monitor traffic interface
fxp0
command, waits for 15 seconds, and then retrieves and returns the
command output.
from jnpr.junos import Device from jnpr.junos.utils.start_shell import StartShell from pprint import pprint dev = Device(host='router1.example.net') with StartShell(dev) as ss: pprint(ss.run('cli -c "monitor traffic interface fxp0"', this=None, timeout=15))
Change History Table
Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.
StartShell
instance connects to the same port that is
defined in the Device
instance, except when the host is set to
localhost or the port is 830 or undefined.