Help with OV CLI Scripting & Javascript

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?

Just tested with OV Release 4.8R2 Build 21, and an OmniSwitch running 8.9.94.R4 - the value of “cli.lastResponse()” gives me the correct output of “show chassis chassis-id 1”. So I can say with these releases it works as expected.

As far is I know, there is no more documentation as it can be found in the help menu of OV2500 itself. If you look for more examples, try omnivista-scripting [DokuWiki] .

If you got a specific problem with a script, please share - I can try to support.

Ignore this reply. See below

After reading another 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(). So, I actually am getting the output, it’s just below the original command which is also included at the top of the output.

1 Like

Perfect! Exactly, you get the command and the output like in the example below:

Thank you for showing me that audit log. I didn’t know it existed. I’ve been looking in the log in the CLI scripting section.

When I do the trace and look at the audit log, I do get the output like you get above. But when I echo the output of cli.lastResponse() in my script, I get output starting with the command itself. This is what was throwing me off. So it seems that the output of cli.lastResponse() does start with the command that was issued followed by the output.

See the attached output from the following script:

<js>
/* @@Test of cliLastResponse() method @@*/
cli.setTimeout(0, 5);
cli.sendCmd("show chassis chassis-id 1");
var response = cli.lastResponse();
cli.trace(response);
cli.sendCmd("echo " + response);
</js>

Ah ok, got it. So if needed, you might need to remove the command from the response. Yep, the Audit Module is a quite nice tool for quickly checking the scripting logs: With cli.trace(“something good”) you get the entry in blue, whereas cli.errorLog(“something wrong”) gives you the entry in red - making it easy to spot results your are looking for.

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:

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 "";
  }
}