fail2ban.server.utils module

class fail2ban.server.utils.Utils

Bases: object

Utilities provide diverse static methods like executes OS shell commands, etc.

Methods

Cache(*args, **kwargs)

A simple cache with a TTL and limit on size

buildShellCmd(realCmd, varsDict)

Generates new shell command as array, contains map as variables to

executeCmd(realCmd[, timeout, shell, ...])

Executes a command.

pid_exists(pid)

Check whether pid exists in the current process table.

wait_for(cond, timeout[, interval])

Wait until condition expression cond is True, up to timeout sec

load_python_module

setFBlockMode

class Cache(*args, **kwargs)

Bases: object

A simple cache with a TTL and limit on size

Methods

clear

get

set

setOptions

unset

clear()
get(k, defv=None)
set(k, v)
setOptions(maxCount=1000, maxTime=60)
unset(k)
DEFAULT_SHORTEST_INTERVAL = 1e-05
DEFAULT_SHORT_INTERVAL = 0.001
DEFAULT_SLEEP_INTERVAL = 0.2
DEFAULT_SLEEP_TIME = 2
static buildShellCmd(realCmd, varsDict)
Generates new shell command as array, contains map as variables to

arguments statement (varsStat), the command (realCmd) used this variables and the list of the arguments, mapped from varsDict

Example:

buildShellCmd(‘echo “V2: $v2, V1: $v1”’, {“v1”: “val 1”, “v2”: “val 2”, “vUnused”: “unused var”})

returns:

[‘v1=$0 v2=$1 vUnused=$2

echo “V2: $v2, V1: $v1”’, ‘val 1’, ‘val 2’, ‘unused var’]

static executeCmd(realCmd, timeout=60, shell=True, output=False, tout_kill_tree=True, success_codes=(0,), varsDict=None)

Executes a command.

Parameters:
realCmdstr

The command to execute.

timeoutint

The time out in seconds for the command.

shellbool

If shell is True (default), the specified command (may be a string) will be executed through the shell.

outputbool

If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode). If False, just indication of success is returned

varsDict: dict

variables supplied to the command (or to the shell script)

Returns:
bool or (bool, str, str, int)

True if the command succeeded and with stdout, stderr, returncode if output was set to True

Raises:
OSError

If command fails to be executed.

RuntimeError

If command execution times out.

static load_python_module(pythonModule)
static pid_exists(pid)

Check whether pid exists in the current process table.

static setFBlockMode(fhandle, value)
static wait_for(cond, timeout, interval=None)

Wait until condition expression cond is True, up to timeout sec

Parameters:
condcallable

The expression to check condition (should return equivalent to bool True if wait successful).

timeoutfloat or callable

The time out for end of wait (in seconds or callable that returns True if timeout occurred).

intervalfloat (optional)

Polling start interval for wait cycle in seconds.

Returns:
variable

The return value of the last call of cond, logical False (or None, 0, etc) if timeout occurred.