x2gobroker.utils module

Here you find a collection of tools that are used by X2Go Session Broker’s code internally.

Everything that is not directly related to specific broker code and potentially reusable at other places in the code tree, is placed into this module.

x2gobroker.utils.compare_versions(version_a, op, version_b)[source]

Compare <version_a> with <version_b> using operator <op>. In the background distutils.version.LooseVersion is used for the comparison operation.

Parameters:
  • version_a (str) – a version string

  • op (str) – an operator provide as string (e.g. ‘<’, ‘>’, ‘==’, ‘>=’ etc.)

  • version_b (str) – another version string that is to be compared with <version_a>

Returns:

if the comparison is True or False

Return type:

bool

x2gobroker.utils.delayed_execution(agent_func, delay, *args, **kwargs)[source]

Delay execution of a function.

Parameters:
  • func (func) – function to be executed.

  • delay (int) – delay of the function start in seconds

  • args (list) – arg parameters to be handed over to the to-be-delayed function

  • kwargs (dict) – kwarg parameters to be handed over to the to-be-delayed function

x2gobroker.utils.drop_privileges(uid, gid)[source]

Drop privileges from super-user root to given <uid> and <gid>.

Only works when run as root, if run with a non-super-user account, None is returned.

If privileges could be dropped, the environment’s HOME variable is adapted to the new user account’s home directory path.

Also, the umask of the account we dropped privileges to is set to 0o077.

Parameters:
  • uid (str) – the user ID of the user account to drop privileges to

  • gid (str) – the group ID to drop privileges to

x2gobroker.utils.get_encoding()[source]

Detect systems default character encoding.

Returns:

The system’s local character encoding.

Return type:

str

x2gobroker.utils.get_key_fingerprint(key)[source]

Retrieve the host key fingerprint of the server to be validated.

Parameters:

key (PKey) – a Python Paramik PKey` object

Returns:

host key fingerprint

Return type:

str

x2gobroker.utils.get_key_fingerprint_with_colons(key)[source]

Retrieve the (colonized) host key fingerprint of the server to be validated.

Parameters:

key (PKey) – a Python Paramik PKey` object

Returns:

host key fingerprint (with colons)

Return type:

str

x2gobroker.utils.matching_hostnames(server_list_a, server_list_b)[source]

Compare two list of servers, if they have matching hostnames.

This function tries to smoothly ship around asymmetric usage of FQDN hostnames and short hostnames in one list.

Parameters:
  • server_list_a (list of str) – list of servers

  • server_list_b (list of str) – list of servers to compare the first list with

Returns:

a sorted list of matching server hostnames (hostnames that appear in both provided server lists.

Returns:

list of str

x2gobroker.utils.normalize_hostnames(servers)[source]

Take a list or dict of servers and check if they match in their domain part and strip the domain part finally off.

E.g., for servers provided as a list (tuple would be ok, too:

['server1', 'server2'] -> ['server1', server2']
['server1.domain1, 'server2.domain1'] -> ['server1', server2']
['server1.domain1, 'server2.domain2'] -> (['server1', server2'], ['domain1', 'domain2']

E.g., for servers provided as a dict:

{'server1': <whatever-params>, 'server2': <whatever-params> } -> {'server1': <whatever-params>, 'server2': <whatever-params> }

{'server1.domain1': <whatever-params>, 'server2.domain1': <whatever-params> } -> {'server1': <whatever-params>, 'server2': <whatever-params> }

{'server1.domain1': <whatever-params>, 'server2.domain2': <whatever-params> } -> ({'server1': <whatever-params>, 'server2': <whatever-params> }, ['domain1', 'domain2']
Parameters:

servers (list, tuple or dict) – a list, tuple or dict hash with either server hostnames as items or dictionary keys

Returns:

a list or a dict with server domains stripped of the items / keys

Return type:

list, dict or tuple

x2gobroker.utils.portscan(addr, port=22)[source]

Perform a port scan to the requested hostname.

Parameters:
  • addr (str) – address (IPv4, IPv6 or hostname) of the host we want to probe

  • port (int) – port number (default: 22)

Returns:

True if the port is in use, else False (also on errors)

Return type:

bool

x2gobroker.utils.split_host_address(host, default_address=None, default_port=22)[source]

Try to split a <host_addr>:<port> expression into hostname and port.

This function is supposed to work with DNS hostnames, IPv4 and IPv6 address.

Both parts (<host_addr> and <port>) can be omitted in the given host string. If so, default_address and default_port come into play.

Parameters:
  • host (str) – an expression like <host_addr>:<port> (where either the host address or the port can be optional)

  • default_address (str) – a fallback host address to be used (default: None)

  • default_port (int) – a fallback port to be used (default: 22)

Returns:

a tuple of host address and port

Return type:

tuple(<host_addr>, <port>)

x2gobroker.utils.touch_file(filename, mode='a')[source]

Imitates the behaviour of the GNU/touch command.

Parameters:
  • filename (str) – name of the file to touch

  • mode (str) – the file mode (as used for Python file objects)