cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1382
Views
0
Helpful
10
Replies

How to send junos shell command with pyats

assadniang
Level 1
Level 1

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

1 Accepted Solution

Accepted Solutions

I am guessing Email pyats-support-ext@cisco.com

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

10 Replies 10

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()

  

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

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'

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()
Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

assadniang
Level 1
Level 1

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

 

 

    ptr513-u210:
        type: router
        os: junos
#        platform: sdwan
#        alias: 'uut'
        credentials:
            default:
                username: root
                password: juniper123
        connections:
            cli:
                protocol: ssh
                ip: f207:a185:172:30:7::513:1

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')

  

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

assadniang
Level 1
Level 1

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

assadniang
Level 1
Level 1

@bigevilbeard 

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

I’m sure you can request this, yes.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Where do I request it?

I am guessing Email pyats-support-ext@cisco.com

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io