Scripting the CLI

The CLI normally runs in interactive mode, where commands can be entered from the keyboard and are run instantly.

It is also possible to run a script of CLI commands without user input. For example, the following script prints out the Web cache statistics, clears some items from the cache and then prints the Web cache statistics again:

System.Stats.getWebCacheEntries

System.Cache.clearCacheContentItems "Intranet" "http" "www.example.com" "*/product/*"

System.Stats.getWebCacheEntries

Save the script to a file on the local machine and then execute it as follows:

$ ZEUSHOME/zxtm/bin/zcli /home/user/webcache-script

The CLI will read the file and run each command found in turn. Providing the script completes successfully, an exit code of 0 (zero) is returned. If an error occurs when running a command, script execution will stop and the CLI will exit with a non-zero return value. However, where --continue is specified at the command line, the script will continue regardless of any errors encountered during processing. In this case the return value is still 0, except where the CLI encounters an internal error of some kind (such as the use of incorrect parameters).

To fully automate a script, the username and password must be specified. This can be achieved in two ways:

1.Store the password in a file and provide this file to the zcli script:

ZEUSHOME/zxtm/bin/zcli --user username --passfile /home/user/password /home/user/webcache-script

The file containing the password should have suitably restricted permissions so that other users cannot read it.

2.Add the connect command to the start of the script, so it becomes:

connect localhost 9090 username password

System.Stats.getWebCacheEntries

System.Cache.clearCacheContentItems "Intranet" "http" "www.example.com" "*/product/*"

System.Stats.getWebCacheEntries

Script Output

The output from scripted CLI commands is slightly different from interactive CLI commands. In interactive mode, the output is formatted and spaced to make it more human-readable, spreading the output over several lines if needed. In scripted mode, the output from each command is written on one line only, without any extra padding or formatting.

The output format is JSON-formatted, to facilitate importing and parsing in other programs. Output from commands that take several inputs (for example, multiple pool names) is not presented in a table to highlight the results for each item.

For example, compare the output from the same command run on interactive and scripted sessions.

Interactively:

[email protected] > Pool.getNodes [ pool1 pool2 ]

{ pool1: [ "serverA:80", "serverB:80" ],

  pool2: [ "serverC:80", "serverD:80" ] }

From within a script:

$ ZEUSHOME/zxtm/bin/zcli --user username --passfile /home/user/password

/home/user/getnodes

[["serverA:80","serverB:80"],["serverC:80","serverD:80"]]

If you want to keep the human-readable output, then run zcli with the --formatoutput argument.