Exscript.util.file module

Utilities for reading data from files.

Exscript.util.file.get_accounts_from_file(filename)[source]

Reads a list of user/password combinations from the given file and returns a list of Account instances. The file content has the following format:

[account-pool]
user1 = cGFzc3dvcmQ=
user2 = cGFzc3dvcmQ=

Note that “cGFzc3dvcmQ=” is a base64 encoded password. If the input file contains extra config sections other than “account-pool”, they are ignored. Each password needs to be base64 encrypted. To encrypt a password, you may use the following command:

python -c 'import base64; print(base64.b64encode("thepassword"))'
Parameters:filename (string) – The name of the file containing the list of accounts.
Return type:list[Account]
Returns:The newly created account instances.
Exscript.util.file.get_hosts_from_csv(filename, default_protocol='telnet', default_domain='', encoding='utf-8')[source]

Reads a list of hostnames and variables from the tab-separated .csv file with the given name. The first line of the file must contain the column names, e.g.:

address testvar1        testvar2
10.0.0.1        value1  othervalue
10.0.0.1        value2  othervalue2
10.0.0.2        foo     bar

For the above example, the function returns two host objects, where the ‘testvar1’ variable of the first host holds a list containing two entries (‘value1’ and ‘value2’), and the ‘testvar1’ variable of the second host contains a list with a single entry (‘foo’).

Both, the address and the hostname of each host are set to the address given in the first column. If you want the hostname set to another value, you may add a second column containing the hostname:

address hostname        testvar
10.0.0.1        myhost  value
10.0.0.2        otherhost       othervalue
Parameters:
  • filename (string) – A full filename.
  • default_protocol (str) – Passed to the Host constructor.
  • default_domain (str) – Appended to each hostname that has no domain.
  • encoding (str) – The encoding of the file.
Return type:

list[Host]

Returns:

The newly created host instances.

Exscript.util.file.get_hosts_from_file(filename, default_protocol='telnet', default_domain='', remove_duplicates=False, encoding='utf-8')[source]

Reads a list of hostnames from the file with the given name.

Parameters:
  • filename (string) – A full filename.
  • default_protocol (str) – Passed to the Host constructor.
  • default_domain (str) – Appended to each hostname that has no domain.
  • remove_duplicates (bool) – Whether duplicates are removed.
  • encoding (str) – The encoding of the file.
Return type:

list[Host]

Returns:

The newly created host instances.

Exscript.util.file.load_lib(filename)[source]

Loads a Python file containing functions, and returns the content of the __lib__ variable. The __lib__ variable must contain a dictionary mapping function names to callables.

Returns a dictionary mapping the namespaced function names to callables. The namespace is the basename of the file, without file extension.

The result of this function can later be passed to run_template:

functions = load_lib('my_library.py')
run_template(conn, 'foo.exscript', **functions)
Parameters:filename (string) – A full filename.
Return type:dict[string->object]
Returns:The loaded functions.