Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/nokia/moler/llms.txt

Use this file to discover all available pages before exploring further.

Unix events are observer objects that run concurrently with commands, scanning connection output line-by-line and firing when a condition is met. Unlike commands, events do not send anything to the connection — they only observe. All Unix events inherit from either GenericUnixLineEvent or GenericUnixTextualEvent.
Events return a list of occurrence dicts. Each occurrence contains line, matched, groups, named_groups, and time. The till_occurs_times parameter controls how many occurrences to collect before the event is considered complete (-1 means run indefinitely).

Event list

ClassModuleWhat it detects
Wait4promptmoler.events.unix.wait4promptA specific shell prompt regex
Wait4promptsmoler.events.unix.wait4promptsOne of multiple prompt regexes, returning state name
PingResponsemoler.events.unix.ping_responseSuccessful ICMP reply lines
PingNoResponsemoler.events.unix.ping_no_responseICMP no-answer / unreachable lines
LastLoginmoler.events.unix.last_loginLast login: ... from ...
LastFailedLoginmoler.events.unix.last_failed_loginLast failed login messages
FailedLoginCountermoler.events.unix.failed_login_counterFailed login count lines
Shutdownmoler.events.unix.shutdownSystem shutdown announcement
WarningDefaultPasswordmoler.events.unix.warning_default_passwordDefault-password warnings
AdviseToChangeYourPasswordmoler.events.unix.advise_to_change_your_passwordPassword change advisories
PrivateSystemmoler.events.unix.private_systemPrivate system / authorised-use notices
UBootCrtmmoler.events.unix.u_boot_crtmU-Boot CRTM boot sequence output

Detailed reference

Wait4promptmoler.events.unix.wait4prompt

The most commonly used Unix event. Waits for a specific prompt pattern to appear on the connection. Internally delegates to Wait4 with match='any'.
connection
object
required
Moler connection to observe.
prompt
str | re.Pattern
required
Regex pattern to match the expected prompt.
till_occurs_times
int
How many times the prompt must be seen before the event completes. Default -1 (run indefinitely).
runner
object
Runner used to execute the event.
Result list item keys
KeyTypeDescription
linestrThe full line that matched
matchedstrThe substring matched by the prompt regex
groupstupleUnnamed capture groups from the regex
named_groupsdictNamed capture groups from the regex
timedatetimeTimestamp when the match occurred
from moler.events.unix.wait4prompt import Wait4prompt

event = Wait4prompt(
    connection=conn,
    prompt=r'host:.*#',
    till_occurs_times=1,
)
result = event()
# result = [
#   {
#     'line': 'host:~ #',
#     'matched': 'host:~ #',
#     'groups': (),
#     'named_groups': {},
#     'time': datetime.datetime(2024, 1, 14, 13, 12, 48, 224929)
#   }
# ]

Wait4promptsmoler.events.unix.wait4prompts

Like Wait4prompt, but watches for multiple possible prompts simultaneously and returns the prompt’s state name alongside the match. Useful for state-machine-based connection management.
connection
object
required
Moler connection to observe.
prompts
dict
required
Mapping of prompt regex (str or compiled) → state name string. E.g. {r'host:.*#': 'UNIX_ROOT', r'host:.*\$': 'UNIX_USER'}.
till_occurs_times
int
Number of occurrences before completion. Default -1.
Result list item keys
KeyTypeDescription
linestrFull matched line
matchedstrMatched substring
prompt_regexstrPattern string of the matching prompt
statestrState name from the prompts dict
timedatetimeTimestamp of the match
from moler.events.unix.wait4prompts import Wait4prompts

event = Wait4prompts(
    connection=conn,
    prompts={
        r'admin@switch#': 'JUNIPER_CONFIG',
        r'admin@switch>': 'JUNIPER_CLI',
    },
    till_occurs_times=1,
)
result = event()
print(result[0]['state'])         # 'JUNIPER_CLI'
print(result[0]['prompt_regex'])  # 'admin@switch>'
You can dynamically update the prompt set while the event is running:
event.change_prompts({r'new_prompt:~\$': 'NEW_STATE'})

PingResponsemoler.events.unix.ping_response

Fires whenever a successful ICMP reply line ("N bytes from ..." ) appears in the output. Useful for monitoring connectivity while a long-running ping command executes.
connection
object
required
Moler connection to observe.
till_occurs_times
int
Number of occurrences. Default -1.
Detection pattern: r'\d+ bytes from.+'
from moler.events.unix.ping_response import PingResponse

event = PingResponse(connection=conn, till_occurs_times=3)
result = event()
# result has 3 entries, each with the icmp_seq reply line
for entry in result:
    print(entry['line'])  # e.g. '64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.6 ms'

PingNoResponsemoler.events.unix.ping_no_response

Fires on no answer yet for icmp_seq=N or Destination Host Unreachable lines.
connection
object
required
Moler connection to observe.
till_occurs_times
int
Number of occurrences. Default -1.
Detection pattern: r'(no answer yet for.*)|(.*Destination Host Unreachable)'

LastLoginmoler.events.unix.last_login

Parses Last login: <date> from <host> lines into structured data. Result list item keys
KeyTypeDescription
timedatetimeTime the event was detected
hoststrSource host/IP from the login message
date_rawstrRaw date string from the message
datedatetimeParsed datetime of the last login
from moler.events.unix.last_login import LastLogin

event = LastLogin(connection=conn, till_occurs_times=1)
result = event()
print(result[0]['host'])  # '127.0.0.1'
print(result[0]['date'])  # datetime(2018, 6, 12, 8, 54, 44)

Shutdownmoler.events.unix.shutdown

Detects system shutdown announcements matching system is going down for <reason> at <time>. Result list item keys: line, matched, groups (tuple of (reason, time_string)), named_groups, time.
from moler.events.unix.shutdown import Shutdown

event = Shutdown(connection=conn, till_occurs_times=1)
result = event()
reason, at_time = result[0]['groups']
print(f"System going down for {reason} at {at_time}")
# System going down for reboot at Tue 2024-03-19 12:15:16 CET!

Using events with callbacks

Events can be used either synchronously (blocking until till_occurs_times is reached) or asynchronously with callbacks:
from moler.events.unix.ping_response import PingResponse
from moler.events.unix.ping_no_response import PingNoResponse

responses = []
no_responses = []

def on_response(event_data):
    responses.append(event_data)
    print(f"Ping OK: {event_data['line']}")

def on_no_response(event_data):
    no_responses.append(event_data)
    print(f"Ping FAIL: {event_data['line']}")

# Start both events in background
ok_event = PingResponse(connection=conn)
ok_event.add_event_occurred_callback(on_response)
ok_event.start()

fail_event = PingNoResponse(connection=conn)
fail_event.add_event_occurred_callback(on_no_response)
fail_event.start()

# ... run ping command ...

ok_event.cancel()
fail_event.cancel()
print(f"Got {len(responses)} replies, {len(no_responses)} failures")