trigger.gorc — Determine commands to run upon login

This is used by ../usage/scripts/go to execute commands upon login to a device. A user may specify a list of commands to execute for each vendor. If the file is not found, or the syntax is bad, no commands will be passed to the device.

By default, only a very limited subset of root commands are allowed to be specified within the .gorc. Any root commands not explicitly permitted will be filtered out prior to passing them along to the device.

The only public interface to this module is get_init_commands. Given a .gorc That looks like this:

[init_commands]
cisco:
    term mon
    terminal length 0
    show clock

This is what is returned:

>>> from trigger import gorc
>>> gorc.get_init_commands('cisco')
['term mon', 'terminal length 0', 'show clock']

You may also pass a list of commands as the init_commands argument to the connect function (or a NetDevice object’s method of the same name) to override anything specified in a user’s .gorc:

>>> from trigger.netdevices import NetDevices
>>> nd = NetDevices()
>>> dev = nd.find('foo1-abc')
>>> dev.connect(init_commands=['show clock', 'exit'])
Connecting to foo1-abc.net.aol.com.  Use ^X to exit.

Fetching credentials from /home/jathan/.tacacsrc
foo1-abc#show clock
22:48:24.445 UTC Sat Jun 23 2012
foo1-abc#exit
>>>
trigger.gorc.filter_commands(cmds, allowed_commands=None)

Filters root commands from cmds that are not explicitly allowed.

Allowed commands are defined using GORC_ALLOWED_COMMANDS.

Parameters:
  • cmds – A list of commands that should be filtered
  • allowed_commands – A list of commands that are allowed
Returns:

Filtered list of commands

trigger.gorc.get_init_commands(vendor)

Return a list of init commands for a given vendor name. In all failure cases it will return an empty list.

Parameters:vendor – A vendor name (e.g. ‘juniper’)
Returns:list of commands
trigger.gorc.parse_commands(vendor, section='init_commands', config=None)

Fetch the init commands.

Parameters:
  • vendors – A vendor name (e.g. ‘juniper’)
  • section – The section of the config
  • config – A parsed ConfigParser object
Returns:

List of commands

trigger.gorc.read_config(filepath='/home/docs/.gorc')

Read the .gorc file

Parameters:filepath – The path to the .gorc file
Returns:A parsed ConfigParser object