Command Line Interface (CLI)¶
A module that implements a CLI.
- munet.cli.host_in(restr, names)¶
Determine if matcher is a regex that matches one of names.
- munet.cli.expand_host(restr, names)¶
Expand name or regexp into list of hosts.
- munet.cli.expand_hosts(restrs, names)¶
Expand list of host names or regex into list of hosts.
- munet.cli.proc_readline(fd, prompt, histfile)¶
Read a line of input from user while running in a sub-process.
- async munet.cli.async_input_reader(rfd)¶
Read a line of input from the user input sub-process pipe.
- async munet.cli.async_input(prompt, histfile)¶
Asynchronously read a line from the user.
- async munet.cli.run_command(unet, outf, line, execfmt, banner, hosts, toplevel, kinds, ns_only=False, interactive=False)¶
Runs a command on a set of hosts.
Runs execfmt. Prior to executing the string the following transformations are performed on it.
execfmt may also be a dictionary of dicitonaries keyed on kind with exec holding the kind’s execfmt string.
- if {} is present then str.format is called to replace {} with any extra
input values after the command and hosts are removed from the input.
else if any {digits} are present then str.format is called to replace {digits} with positional args obtained from the addittional user input first passed to shlex.split.
else f-string style interpolation is performed on the string with the local variables host (the current node object or None), unet (the Munet object), and user_input (the additional command input) defined.
The output is sent to outf. If ns_only is True then the execfmt is run using Commander.cmd_status_nsonly otherwise it is run with Commander.cmd_status.
- class munet.cli.Completer(unet)¶
A completer class for the CLI.
- async munet.cli.cli_client(sockpath, prompt='munet> ')¶
Implement the user-facing CLI for a remote munet reached by a socket.
- async munet.cli.local_cli(unet, outf, prompt, histfile, background)¶
Implement the user-side CLI for local munet.
- async munet.cli.cli_client_connected(unet, background, reader, writer)¶
Handle CLI commands inside the munet process from a socket.
- async munet.cli.remote_cli(unet, prompt, title, background, remote_wait=False)¶
Open a CLI in a new window.
- munet.cli.add_cli_in_window_cmd(unet, name, helpfmt, helptxt, execfmt, toplevel, kinds, **kwargs)¶
Adds a CLI command to the CLI.
The command cmd is added to the commands executable by the user from the CLI. See base.Commander.run_in_window for the arguments that can be passed in args and kwargs to this function.
- Parameters:
unet – unet object
name – command string (no spaces)
helpfmt – format of command to display in help (left side)
helptxt – help string for command (right side)
execfmt – interpreter cmd to pass to host.run_in_window(), if {} present then allow for user commands to be entered and inserted. May also be a dict of dict keyed on kind with sub-key of “exec” providing the execfmt string for that kind.
toplevel – run command in common top-level namespaec not inside hosts
kinds – limit CLI command to nodes which match list of kinds.
**kwargs – keyword args to pass to host.run_in_window()
- munet.cli.add_cli_run_cmd(unet, name, helpfmt, helptxt, execfmt, toplevel, kinds, ns_only=False, interactive=False)¶
Adds a CLI command to the CLI.
The command cmd is added to the commands executable by the user from the CLI. See run_command above in the doline function and for the arguments that can be passed in to this function.
- Parameters:
unet – unet object
name – command string (no spaces)
helpfmt – format of command to display in help (left side)
helptxt – help string for command (right side)
execfmt – format string to insert user cmds into for execution. May also be a dict of dict keyed on kind with sub-key of “exec” providing the execfmt string for that kind.
toplevel – run command in common top-level namespaec not inside hosts
kinds – limit CLI command to nodes which match list of kinds.
ns_only – Should execute the command on the host vs in the node namespace.
interactive – Should execute the command inside an allocated pty (interactive)
- munet.cli.add_cli_config(unet, config)¶
Adds CLI commands based on config.
All exec strings will have %CONFIGDIR%, %NAME% and %RUNDIR% replaced with the corresponding config directory and the current nodes name and rundir. Additionally, the exec string will have f-string style interpolation performed with the local variables host (node object or None), unet (Munet object) and user_input (if provided to the CLI command) defined.
The format of the config dictionary can be seen in the following example. The first list entry represents the default command because it has no name key.
- commands:
help: “run the given FRR command using vtysh” format: “[HOST …] FRR-CLI-COMMAND” exec: “vtysh -c {}” ns-only: false # the default interactive: false # the default
name: “vtysh” help: “Open a FRR CLI inside new terminal[s] on the given HOST[s]” format: “vtysh HOST [HOST …]” exec: “vtysh” new-window: true
name: “capture” help: “Capture packets on a given network” format: “pcap NETWORK” exec: “tshark -s 9200 -i {0} -w /tmp/capture-{0}.pcap” new-window: true top-level: true # run in top-level container namespace, above hosts
The new_window key can also be a dictionary which will be passed as keyward arguments to the Commander.run_in_window() function.
- Parameters:
unet – unet object
config – dictionary of cli config