Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


Use Junos PyEZ to Access the Shell on Junos Devices

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 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 only requires access to the shell, it can omit the calls to the Device open() and close() methods.

Starting in Junos PyEZ Release 2.6.4, Startshell supports the shell_type option within remote scripts to specify the shell type. By default, Startshell instances are type C Shell (csh). You can also specify shell_type="sh" to start a Bourne-style shell (ash).

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. If you set the this="string" argument to a specific string, the method waits for the expected string or pattern before returning the command output. The return value is a tuple, where the first item is True if the exit code is 0, and False otherwise, and 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.

In this example, 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.

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:

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 in the Junos OS shell before timing out. If you do not specify a timeout, the default is 30 seconds.

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 OS 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.