Help with OV CLI Scripting & Javascript

Post Reply
ssherman68
Member
Posts: 3
Joined: 28 Mar 2024 13:10

Help with OV CLI Scripting & Javascript

Post by ssherman68 »

I’m trying to use OV CLI scripting with Javascript. I’m trying to use the cli.lastResponse method to gather the output of the show chassis chassis-id x command. According to the OV 2500 4.7 User Guide this is supposed to “returns a string that represents the switch output from the last command the user sent to the switch”. However, I’m finding that it’s returning the last command I sent to the switch and not the output of the command. So, cli.lastResponse(); is giving me “show chassis chassis-id 1”.

Is this a bug or a documentation error? If it’s the second, how do I get the output of the last command using CLI scripting?

I’m also finding other anomalies. Among them:

-CLI scripting errors when using the // command delimiter in js code
-It errors when trying to declare an array as a const rather than a var
-It errors on the println(): command given in the documentation

Is there any more thorough or updated documentation for CLI scripting using Javascript?
silvio
Alcatel Unleashed Certified Guru
Alcatel Unleashed Certified Guru
Posts: 1896
Joined: 01 Jul 2008 10:51
Location: Germany

Re: Help with OV CLI Scripting & Javascript

Post by silvio »

I have seen it also sometimes that the help for the scripting is okay for release 6 switches but not for AOS R8.
One more example you can find at https://dokuwiki.alu4u.com/doku.php?id= ... -scripting - but also for R6.
Probably you need to open a ticket at ALE to get help with the correct command.
BR Silvio
HStr
Member
Posts: 8
Joined: 21 Apr 2022 05:20

Re: Help with OV CLI Scripting & Javascript

Post by HStr »

Hi ssherman68,

Please keep in mind that cli.lastResponse() is returning both last command and result as shown in attachment.

Regards Holger
Clipboard01.jpg
ssherman68 wrote: 28 Mar 2024 14:05 I’m trying to use OV CLI scripting with Javascript. I’m trying to use the cli.lastResponse method to gather the output of the show chassis chassis-id x command. According to the OV 2500 4.7 User Guide this is supposed to “returns a string that represents the switch output from the last command the user sent to the switch”. However, I’m finding that it’s returning the last command I sent to the switch and not the output of the command. So, cli.lastResponse(); is giving me “show chassis chassis-id 1”.

Is this a bug or a documentation error? If it’s the second, how do I get the output of the last command using CLI scripting?

I’m also finding other anomalies. Among them:

-CLI scripting errors when using the // command delimiter in js code
-It errors when trying to declare an array as a const rather than a var
-It errors on the println(): command given in the documentation

Is there any more thorough or updated documentation for CLI scripting using Javascript?
You do not have the required permissions to view the files attached to this post.
ssherman68
Member
Posts: 3
Joined: 28 Mar 2024 13:10

Re: Help with OV CLI Scripting & Javascript

Post by ssherman68 »

Thank you Holger. You're right. After looking at your reply re-running my test script and re-interpreting the output of the cli script log it actually is outputting the command and then the results of the command below. I was mis-interpreting those results as an artifact of the script logging and not the actual output of cli.lastResponse().

Silvio, thank you for that website. I'll bookmark it and look at it more closely to get some ideas on how to better do these scripts.
ssherman68
Member
Posts: 3
Joined: 28 Mar 2024 13:10

Re: Help with OV CLI Scripting & Javascript

Post by ssherman68 »

After a bunch of learning and testing I came up with a function that issues a command, takes the response, strips out the command and extraneous characters & also does some error checking. I decided to split the command into 3 variables: 1. the command itself, 2. input to the command such as a port or BGP AS and 3. any piped filter that comes after the command ( | grep foo). That way I could do different things with them later on if needed. For example I could add a check for AOS 6 and remove the filter since 6 doesn’t support it. I’m sure some improvements can be made as I come up with other cases.

The important part is to slice out the length of the command + 2 characters and the length of the entire output minus 2 characters and then trim whitespace off of that to be sure.

Here it is:

Code: Select all

function cliCmdResponse(cmd, cmd_input, cmd_filter)
{ /* Takes a cli command, input (i.e. port list, etc.), filter (i.e. '| grep xx') */
  /* sends it to the device, checks the response and removes the command & the */
  /* extra characters from the response. Generates a log entry and returns  */
  /* the response. The function was not built to handle commands that require */
  /* input to prompts after issuing the command (i.e. reload */
  
  var command = cmd + " " + cmd_input + " " + cmd_filter;
  cli.sendCmd(command);
  var output = cli.lastResponse();
  /* remove the original command & extraneous chars */
  var response = output.slice(command.length + 2, output.length - 2).trim(); 
  /* If the first 12 chars contain "ERROR:" the switch threw an error */
  if ( response.slice(0,12).search("ERROR:") > -1 ) {
    cli.errorLog("Command " + command + " Generated an error: " + response);
    return "";
  } 
  else if ( response.length > 0 ) { /* Valid Response */
    cli.trace("INFO: Command " + command + " Response: " + response);
    return response;
  } 
  else { /* Sometimes the grep filters everything out. Handle this case */
    cli.trace("INFO: Command " + command + " generated no output");
    return "";
  }
}
Post Reply

Return to “OmniVista 2500 v4.x”