ahojte,
chcem docielit, aby pre openvpn klientov turris vyrobil prislusny DNS zaznam (napr. v kresd pomocou statickej tabulky hosts.custom
), mazanie po pripadnom odpojeni nie je dolezite. napadlo mi parsovat openvpn-status.log
napr. skriptom nizsie, je to vsak take riesenie-neriesenie. napada niekomu nieco systemovejsie?
dakujem, jose
from datetime import datetime
import time
import re
import os
STATUS_FILE = '/tmp/openvpn-status.log'
HOSTS_FILE = '/etc/kresd/hosts.custom'
GMTIMEDIFF = 7200
with open(STATUS_FILE) as f:
data = f.read()
host_data = re.search('Last Ref.(.+).GLOBAL STATS.*', data, re.S).group(1)
host_data = host_data.splitlines()
conn_data = re.search('Connected Since.(.+).ROUTING TABLE.*', data, re.S).group(1)
conn_data = conn_data.splitlines()
latest_client = max([int(datetime.strptime(x.split(',')[4], '%c').strftime('%s')) for x in conn_data])
latest_client -= GMTIMEDIFF
try:
latest_update = int(time.strftime('%s', time.gmtime(os.stat(HOSTS_FILE).st_mtime)))
except:
latest_update = 0
print(latest_update)
print(latest_client)
if latest_update < latest_client:
# we have a new client connected since last HOSTS_FILE modification
print('Updating..')
with open(HOSTS_FILE, 'w') as f:
# populate HOSTS_FILE with entries from openvpn-status.log
for entry in host_data:
ip, hostname, ip2, date = entry.strip().split(',')
f.write('%s %s\n' % (ip, hostname))