KnownHosts

Known hosts backend.

cumin.backends.knownhosts.grammar()[source]

Define the query grammar.

Some query examples:

  • Simple selection: host1.domain

  • ClusterShell syntax for hosts expansion: host10[10-42].domain,host2010.other-domain

  • ClusterShell syntax for hosts globbing: host10[10-42]*

  • A complex selection: host100[1-5]* or (host10[30-40].domain and (host10[10-42].domain and not host33.domain))

Backus-Naur form (BNF) of the grammar:

<grammar> ::= <item> | <item> <boolean> <grammar>
   <item> ::= <hosts> | "(" <grammar> ")"
<boolean> ::= "and not" | "and" | "xor" | "or"

Given that the pyparsing library defines the grammar in a BNF-like style, for the details of the tokens not specified above check directly the source code.

Returns:

the grammar parser.

Return type:

pyparsing.ParserElement

exception cumin.backends.knownhosts.KnownHostsLineError[source]

Bases: InvalidQueryError

Custom exception class for invalid lines in SSH known hosts files.

exception cumin.backends.knownhosts.KnownHostsSkippedLineError[source]

Bases: InvalidQueryError

Custom exception class for skipped lines in SSH known hosts files.

class cumin.backends.knownhosts.KnownHostsQuery(config)[source]

Bases: BaseQueryAggregator

KnownHostsQuery query builder.

The knownhosts backend allow to use Cumin taking advantage of existing SSH known hosts files that are not hashed. It allow to write arbitrarily complex queries with subgroups and boolean operators, but each item must be either the hostname itself, or using host expansion with the powerful ClusterShell.NodeSet.NodeSet syntax.

The typical use case for the knownhosts backend is when the known hosts file(s) are generated and kept updated by some external configuration manager or tool that is not yet supported as a backend for Cumin. It can also work as a fallback backend in case the primary backend is unavailable but the known hosts file(s) are still up to date.

Known hosts query constructor, initialize the known hosts.

Parameters:

according to parent cumin.backends.BaseQuery.__init__().

grammar = Forward: {Group:({{~{{{{'and not' | 'and'} | 'xor'} | 'or'}} W:(!&*,-.0-9?A-[...)} | {{'(' : ...} ')'}}) [Group:({{'and not' | 'and'} | 'xor'} | 'or'} {{~{{{{'and not' | 'and'} | 'xor'} | 'or'}} W:(!&*,-.0-9?A-[...)} | {{'(' : ...} ')'})]...}

load the grammar parser only once in a singleton-like way.

Type:

pyparsing.ParserElement

_build(query_string)[source]

Override parent method to lazy-loading the known hosts if needed.

Parameters:

according to parent cumin.backends.BaseQuery._build().

_execute()[source]

Override parent method to ensure to return only existing hosts.

Parameters:

according to parent cumin.backends.BaseQuery._execute().

_parse_token(token)[source]

Concrete implementation of parent abstract method.

Parameters:

according to parent cumin.backends.BaseQueryAggregator._parse_token().

_load_known_hosts()[source]

Load all known hosts file listed in the configuration.

static parse_known_hosts_line(line)[source]

Parse an SSH known hosts formatted line and extract the valid hostnames.

See the SSH_KNOWN_HOSTS FILE FORMAT` in ``man sshd for the details of the file format.

Parameters:

line (str) -- the line to parse.

Raises:
Returns:

a set with the hostnames found in the given line.

Return type:

set

static parse_line_hosts(line_hosts)[source]

Parse a comma-separated hostnamed from an SSH known hosts formatted line and extract the valid hostnames.

Parameters:

line_hosts (str) -- the hostnames to parse.

Returns:

a tuple with two sets, the hostnames found in the given line and the hostnames skipped.

Return type:

tuple

cumin.backends.knownhosts.GRAMMAR_PREFIX = 'K'

the prefix associate to this grammar, to register this backend into the general grammar. Required by the backend auto-loader in cumin.grammar.get_registered_backends().

Type:

str

cumin.backends.knownhosts.query_class

Required by the backend auto-loader in cumin.grammar.get_registered_backends().