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 stringop (
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
orFalse
- 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 secondsargs (
list
) – arg parameters to be handed over to the to-be-delayed functionkwargs (
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 togid (
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 ParamikPKey`
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 ParamikPKey`
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
ofstr
) – list of serversserver_list_b (
list
ofstr
) – 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
ofstr
- x2gobroker.utils.normalize_hostnames(servers)[source]¶
Take a
list
ordict
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
ordict
) – alist
,tuple
ordict
hash with either server hostnames as items or dictionary keys- Returns
a
list
or adict
with server domains stripped of the items / keys- Return type
list
,dict
ortuple
- 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 probeport (
int
) – port number (default: 22)
- Returns
True
if the port is in use, elseFalse
(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
anddefault_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>)