iagctl run service python-script

Run a Python script service.

The iagctl run service python-script command executes a Python script service and displays the resulting stdout, stderr, return code, and execution time information.

Syntax

$iagctl run service python-script <service-name> [flags]

Usage notes

Runtime arguments

Use the --set flag to pass arguments to the Python script. IAG passes each value to the script in --key=value format. For example, this command:

$iagctl run service python-script my-script \
> --set device=10.0.0.1 \
> --set commands='["show ver"]'

Causes IAG to run the following in the virtual environment:

$python main.py --device=10.0.0.1 --commands='["show ver"]'

If you defined a decorator during service creation, IAG validates all --set values against it before passing them to the script.

You can also pass a bare --set key without a value. How IAG passes this to your script depends on whether the service has a decorator with the property defined as boolean:

$# Pass a boolean flag
$iagctl run service python-script my-script --set verbose --set host=10.0.0.1
$
$# Omit the boolean flag
$iagctl run service python-script my-script --set host=10.0.0.1

Your script parses these arguments using Python’s argparse module. The following example handles string values, JSON inputs, and a boolean flag:

1import argparse
2import json
3
4def main():
5 parser = argparse.ArgumentParser(description="Run commands on a network device.")
6 parser.add_argument('--device', required=True, help="Device IP address or hostname")
7 parser.add_argument('--commands', required=True, help="Commands to run.")
8 parser.add_argument('--verbose', action='store_true', help="Enable verbose output")
9 args = parser.parse_args()
10
11 device = args.device
12 commands_input = args.commands
13
14 try:
15 commands = json.loads(commands_input)
16 except json.JSONDecodeError:
17 commands = [commands_input]
18
19 if args.verbose:
20 print(f"Connecting to {device} with verbose output enabled")
21
22 print(device)
23 print(commands)
24
25if __name__ == "__main__":
26 main()

View accepted inputs

Use the --use flag with the service name to display information about the inputs a service accepts:

$iagctl run service python-script <service-name> --use

Examples

Run a Python script service

$iagctl run service python-script my-python-service

Run a Python script service with arguments

The following example passes device and commands arguments to the script.

$iagctl run service python-script my-python-service \
>--set device=10.0.0.1 \
>--set commands='["show ver"]'

Options

$ --profile string Specify the client profile to use (case-insensitive, defaults to [client] section)
$ -h, --help Help for python-script
$ --set stringArray Runtime input arguments. Use key=value format for string arguments, or bare key format (--set key) for boolean arguments defined in a decorator schema.
$ --use Display usage of the service.

Options inherited from parent commands

$ --profile string Specify the client profile to use (case-insensitive, defaults to [client] section)
$ --config string Path to the configuration file
$ --raw Display the result of the command in raw format
$ --verbose Enable verbose output