cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
175
Views
0
Helpful
1
Replies

Detect CLI style in python called from actionpoint

IvanDrmic4672
Level 1
Level 1

Hello,

We have a model that uses actionpoint to call python code. Python code is located in packet and it adds commands to configuration and print out some messages in a user session (cli) that called this actionpoint. Actionpoint is registered as:

 

class Main(ncs.application.Application):
    def setup(self):
        # The application class sets up logging for us. It is accessible
        # through 'self.log' and is a ncs.log.Log instance.
        self.log.info('Main RUNNING')
        self.register_action('provision-service', ProvisionService)

 

Class "ProvisionService" does needed work. Snippet from definition of this class:

 

class ProvisionService(ncs.dp.Action):
    .dp.Action.action
    def cb_action(self, uinfo, name, kp, input_params, output, trans):
        
        # Increase timeout
        _ncs.dp.action_set_timeout(uinfo, 600)

        # Helper functions
        printcli = lambda x: _ncs.maapi.cli_write(trans.maapi.msock, uinfo.usid, f'{x}\n')
        clicmd = lambda x: _ncs.maapi.cli_cmd(trans.maapi.msock, uinfo.usid, x)

 

 

We use low level maapi functions "cli_write" and "cli_cmd" to print messages to user session and to run add commands in user session (set up configuration for commit). 

Problem is that commands entered in "cli_cmd" need to match CLI style of user. If user uses Juniper CLI we need to enter commands in Juniper format ("set") and if user uses Cisco CLI we need to enter Cisco style commands.

How can I detect which CLI style is used by user that called this actionpoint?

Side note:

If we use actionpoint for auto completion, NSO will automatically pass cli_style to function:

 

class DeviceEndpointCompletion(ncs.dp.Action):

    def cb_completion(self, uinfo, cli_style, token, completion_char, kp,
                      cmdpath, cmdparam_id, simple_type, extra):

 

We need this "cli_style" data in "cb_action" function.

Thank you for the help. 

 

 

1 Reply 1

snovello
Cisco Employee
Cisco Employee

I don't think CLI style is available but maybe consider trying a harmless quick command and match patterns in the response? Concretely e.g. Stop a non existent job

admin@ncs> request job stop -1
[ok][2024-11-14 11:15:54]
admin@ncs> switch cli
admin@ncs# request job stop -1
-----------^
syntax error: expecting
  autowizard           - Automatically query for mandatory elements

From the response you see which mode you are in.

Alternatively maybe you can explain what you are trying to achieve and why it has to be implemented through CLI commands rather than using any other mechanism.