10-20-2023 12:10 AM
How do I send a command to the junos shell with pyats
junos>start shell
junos#cd /var/public
-----------------------------------------
juniper_device = testbed.devices['ptr513-u210']
juniper_device.connect()
juniper_device.execute('cd /var/public') <--- this fails
Solved! Go to Solution.
01-27-2024 12:04 AM
I am guessing Email pyats-support-ext@cisco.com
10-20-2023 12:36 AM
I think your error is due to command being sent to the wrong CLI mode? Here is an example i found how you can do this.
You.connect to the Junos device using the PyATS Testbed and Device objects. Once establishing the connection, call the device.shell() method to open the Junos shell. Then, you can send commands to the shell using the device.execute() method, specifying cli_mode='junos' to direct the commands to the Junos shell instead of the default vty shell.
Call device.execute('exit') to exit the Junos shell when you are done running commands. Always disconnect from the device using the disconnect() method to close the connection cleanly.
from pyats.topology import Testbed, Device
testbed = Testbed('testbed.yaml')
device = testbed.devices['ptr513-u210']
device.connect()
# Open Junos shell
device.shell()
# Send command to Junos shell
device.execute('cd /var/public', cli_mode='junos')
# Exit shell
device.execute('exit', cli_mode='junos')
device.disconnect()
10-20-2023 07:11 AM
Hi @bigevilbeard
I am getting this error message:
pyats==23.8
>>> import yaml
>>> from pyats.topology import loader
>>> from pyats.topology import Testbed, Device
>>> testbed = loader.load('devices.yaml')
>>> jdm_device = testbed.devices['ptr513-u210']
>>> jdm_device.connect()
>>> jdm_device.shell()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/pyats/topology/device.py", line 559, in pyats.topology.device.Device.__getattr__
AttributeError: 'Device' object has no attribute 'shell'
10-20-2023 07:16 AM
Hmmm i wonder if Juniper device does not support the shell() method in PyATS? Try adding the line jdm_device.os = 'junos' before connecting, and shell() should work as expected.
testbed = Testbed('testbed.yaml')
jdm_device = testbed.devices['ptr513-u210']
jdm_device.os = 'junos'
jdm_device.connect()
jdm_device.shell()
10-20-2023 07:25 AM
Same error message
The os for that device was already set to junos in .yalm file
>>> jdm_device.os = 'junos'
>>> jdm_device.connect()
2023-10-20 10:22:04,978: %UNICON-INFO: ptr513-u210 is already connected
>>> jdm_device.shell()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/pyats/topology/device.py", line 559, in pyats.topology.device.Device.__getattr__
AttributeError: 'Device' object has no attribute 'shell'
>>>
10-20-2023 08:07 AM
Right, if the OS is already specified as Junos in the testbed YAML file, which would imply shell isnt supported and only works for IOS XE and NXOS in PyATS. Would somthing like this work? (i am not a JUNOS user btw)
jdm_device.execute('cli', cli_mode='junos')
jdm_device.execute('show version', cli_mode='junos')
jdm_device.execute('exit', cli_mode='junos')
10-20-2023 08:42 AM
That did not work...
>>> jdm_device.execute('cli', cli_mode='junos')
2023-10-20 11:39:25,294: %UNICON-INFO: +++ ptr513-u210 with via 'cli': executing command 'cli' +++
cli
root@ptr513-u210>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/unicon/bases/routers/services.py", line 259, in unicon.bases.routers.services.BaseService.__call__
File "/opt/labs-scripts/common-python39-venv/lib64/python3.9/site-packages/unicon/plugins/generic/service_implementation.py", line 730, in call_service
dialog_match = dialog.process(
File "src/unicon/eal/dialogs.py", line 476, in unicon.eal.dialogs.Dialog.process
File "src/unicon/eal/dialog_processor.py", line 321, in unicon.eal.dialog_processor.SimpleDialogProcessor.process
File "src/unicon/eal/dialog_processor.py", line 235, in unicon.eal.dialog_processor.SimpleDialogProcessor.expect_eval_statements
File "/opt/labs-scripts/common-python39-venv/lib64/python3.9/site-packages/unicon/plugins/generic/service_implementation.py", line 65, in invalid_state_change_action
raise StateMachineError(msg)
unicon.core.errors.StateMachineError: Expected device to reach 'shell' state, but landed on 'enable' state.
>>>
------------------------------------------------------------------------
I was thinking if we can tell the code to expect (#) after we send the command, like in netmiko
something like this?
>>> jdm_device.execute('start shell', expect '#')
---------------------------------------------------------
>>> jdm_device.execute('start shell')
It enters 'shell' mode (#), but right after it complains that it expect to be in enable mode
2023-10-20 11:29:03,316: %UNICON-INFO: +++ ptr513-u210 with via 'cli': executing command 'start shell' +++
start shell
root@ptr513-u210:~ #
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/unicon/bases/routers/services.py", line 259, in unicon.bases.routers.services.BaseService.__call__
File "/opt/labs-scripts/common-python39-venv/lib64/python3.9/site-packages/unicon/plugins/generic/service_implementation.py", line 730, in call_service
dialog_match = dialog.process(
File "src/unicon/eal/dialogs.py", line 476, in unicon.eal.dialogs.Dialog.process
File "src/unicon/eal/dialog_processor.py", line 321, in unicon.eal.dialog_processor.SimpleDialogProcessor.process
File "src/unicon/eal/dialog_processor.py", line 235, in unicon.eal.dialog_processor.SimpleDialogProcessor.expect_eval_statements
File "/opt/labs-scripts/common-python39-venv/lib64/python3.9/site-packages/unicon/plugins/generic/service_implementation.py", line 65, in invalid_state_change_action
raise StateMachineError(msg)
unicon.core.errors.StateMachineError: Expected device to reach 'enable' state, but landed on 'shell' state.
>>>
01-26-2024 09:37 AM
Can I request this feature to be added to the next release of PyATS for Junos?
To be able to go into Junos Shell and execute some commands
01-26-2024 12:13 PM
I’m sure you can request this, yes.
01-26-2024 12:16 PM
Where do I request it?
01-27-2024 12:04 AM
I am guessing Email pyats-support-ext@cisco.com
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide