How to use the command 'pyats shell' (with examples)
The pyats shell
command is a utility from Cisco’s pyATS (Python Automation Test Systems) framework that allows users to quickly start an interactive Python shell with pre-loaded packages and configurations to facilitate network automation tasks. It significantly reduces the time spent on setting up an environment and allows immediate access to pyATS libraries and functions for prototyping and debugging. The shell is a versatile tool tailored for network engineers and developers interested in enhancing their workflow efficiency when working with network test beds or automated systems by giving them direct access to the pyATS API in an interactive setting.
Use case 1: Open pyATS shell with a defined Testbed file
Code:
pyats shell --testbed-file path/to/testbed.yaml
Motivation:
In network automation, it is often essential to simulate or interact with a network setup. A Testbed file in pyATS provides a description of network devices, their connections, and other characteristics necessary for testing. By directly loading a Testbed file into the pyATS shell, you provide the shell with immediate access to network configurations, making it possible to run and prototype commands with real device data.
Explanation:
pyats shell
: This initializes the pyATS interactive shell environment.--testbed-file
: This argument allows you to specify the path to a Testbed file. The testbed provides the shell with a structured view of the network environment, representing devices and their network connections.path/to/testbed.yaml
: This is the file path to the YAML-formatted Testbed file, which contains the configurations of various network devices and their attributes.
Example Output:
Upon execution, the pyATS shell would load with the Testbed configurations pre-loaded. You’ll gain access to the device objects within the shell, allowing interactive commands that can query or modify directly on the devices defined in your testbed.
PyATS Shell 21.7 (stable) on Python 3.8.5 | May 7 2021
Type '?' for help
# Devices loaded from testbed:
# device1
# device2
>>> device1.connect()
>>> device1.execute('show version')
...
Use case 2: Open pyATS shell with a defined Pickle file
Code:
pyats shell --pickle-file path/to/pickle.file
Motivation:
A Pickle file in Python stores serialized objects, which allow you to save the state of objects to disk for later retrieval and reuse. In the realm of network automation, the ability to save an environment configuration or a session’s state is invaluable for debugging and prototyping. Using a Pickle file as input for the pyATS shell allows users to quickly load complex, pre-configured scenarios without having to reinitialize settings or re-run scripts.
Explanation:
pyats shell
: Starts the pyATS interactive shell.--pickle-file
: This specifies a path to a Pickle file. Pickle files contain serialized objects, which the shell can directly reinstate within the environment.path/to/pickle.file
: Path to the Pickle file that needs to be loaded, which contains serialized network state objects or pyATS session configurations.
Example Output:
When this command is executed, the shell loads with the predefined objects or environment as stored in the Pickle file. This could be logging outputs, specific variable states, or network responses saved from previous interactions.
PyATS Shell 21.7 (stable) on Python 3.8.5 | May 7 2021
Type '?' for help
# Session restored from pickle:
>>> print(session_data)
# Outputs previously stored session details
Use case 3: Open pyATS with IPython disabled
Code:
pyats shell --no-ipython
Motivation:
The IPython shell provides an advanced interactive environment with numerous useful features such as syntax highlighting and auto-completion. However, there are scenarios where a user might prefer to avoid these features, such as when dealing with overly large outputs or when facing compatibility issues with certain extensions. Opting to disable IPython can lead to a cleaner, more standard Python shell experience, which can be beneficial for users who prefer simplicity or require specific integrations incompatible with IPython.
Explanation:
pyats shell
: Initializes the interactive shell environment of pyATS.--no-ipython
: This flag tells the shell environment to start without the IPython enhancements. The shell will revert to the standard Python REPL interface.
Example Output:
Upon execution, the shell will operate as a standard Python interpreter without the bells and whistles of IPython, focusing on a minimal and direct interaction with the user.
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> # Standard python features available
Conclusion:
These three use cases demonstrate the versatility of the pyats shell
command in facilitating network automation tasks with pre-loaded configurations and session states. Whether you’re starting with a testbed description, loading a serialized state, or prefer a minimalistic shell interface, pyATS provides efficient utilities to streamline your development and testing processes in network environments.