Cisco provides a Python module that provides access to run EXEC and configuration commands. You can display the details of
the Cisco Python module by entering the help() command. The help() command displays the properties of the Cisco CLI module.
The following example displays information about the Cisco Python module:
Device# guestshell run python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> from cli import cli,clip,configure,configurep, execute, executep
>>> help(configure)
Help on function configure in module cli:
configure(configuration)
Apply a configuration (set of Cisco IOS CLI config-mode commands) to the device
and return a list of results.
configuration = '''interface gigabitEthernet 0/0
no shutdown'''
# push it through the Cisco IOS CLI.
try:
results = cli.configure(configuration)
print "Success!"
except CLIConfigurationError as e:
print "Failed configurations:"
for failure in e.failed:
print failure
Args:
configuration (str or iterable): Configuration commands, separated by newlines.
Returns:
list(ConfigResult): A list of results, one for each line.
Raises:
CLISyntaxError: If there is a syntax error in the configuration.
>>> help(configurep)
Help on function configurep in module cli:
configurep(configuration)
Apply a configuration (set of Cisco IOS CLI config-mode commands) to the device
and prints the result.
configuration = '''interface gigabitEthernet 0/0
no shutdown'''
# push it through the Cisco IOS CLI.
configurep(configuration)
Args:
configuration (str or iterable): Configuration commands, separated by newlines.
>>> help(execute)
Help on function execute in module cli:
execute(command)
Execute Cisco IOS CLI exec-mode command and return the result.
command_output = execute("show version")
Args:
command (str): The exec-mode command to run.
Returns:
str: The output of the command.
Raises:
CLISyntaxError: If there is a syntax error in the command.
>>> help(executep)
Help on function executep in module cli:
executep(command)
Execute Cisco IOS CLI exec-mode command and print the result.
executep("show version")
Args:
command (str): The exec-mode command to run.
>>> help(cli)
Help on function cli in module cli:
cli(command)
Execute Cisco IOS CLI command(s) and return the result.
A single command or a delimited batch of commands may be run. The
delimiter is a space and a semicolon, " ;". Configuration commands must be
in fully qualified form.
output = cli("show version")
output = cli("show version ; show ip interface brief")
output = cli("configure terminal ; interface gigabitEthernet 0/0 ; no shutdown")
Args:
command (str): The exec or config CLI command(s) to be run.
Returns:
string: CLI output for show commands and an empty string for
configuration commands.
Raises:
errors.cli_syntax_error: if the command is not valid.
errors.cli_exec_error: if the execution of command is not successful.
>>> help(clip)
Help on function clip in module cli:
clip(command)
Execute Cisco IOS CLI command(s) and print the result.
A single command or a delimited batch of commands may be run. The
delimiter is a space and a semicolon, " ;". Configuration commands must be
in fully qualified form.
clip("show version")
clip("show version ; show ip interface brief")
clip("configure terminal ; interface gigabitEthernet 0/0 ; no shutdown")
Args:
command (str): The exec or config CLI command(s) to be run.