#!/usr/bin/env python3 # # resets the clock on a resumed internet connected openbsd guest running on vmware # # restart ntpd (ntpd_flags="-s") when guest resumes # - make sure current user can "doas rcctl ntpd restart" # # (c) 2020 torh-scrape@bogus.net # # this won't work on openbsd-current (6.6>), as "-s" has been removed # from ntpd; see https://marc.info/?l=openbsd-misc&m=158158413909971&w=2 # from pygtail import Pygtail import re import tempfile from subprocess import DEVNULL, STDOUT, check_call import time import os import signal def sigTerm(signum,fr): os.close(fd) os.remove(tf) exit() if __name__ == '__main__': tailfile="/var/log/messages" m = re.compile(".* /bsd: VMware guest resuming from suspended state.*") fd, tf = tempfile.mkstemp(dir="/tmp") signal.signal(signal.SIGTERM, sigTerm) signal.signal(signal.SIGINT, sigTerm) for line in Pygtail(tailfile, read_from_end=True, offset_file=tf): pass while True: for line in Pygtail(tailfile, read_from_end=True, offset_file=tf): if m.match(line): with open(os.devnull, 'wb') as devnull: try: check_call(['/usr/bin/doas', '/usr/sbin/rcctl', 'restart', 'ntpd'], stdout=DEVNULL) except Exception as e: print(e) time.sleep(1)