2020-12-10 15:14:17 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from ipaddress import ip_address
|
|
|
|
from subprocess import run
|
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
import netifaces
|
|
|
|
|
|
|
|
now = int(datetime.timestamp(datetime.utcnow()))
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('/var/tmp/pppd-last-restart.status', 'r') as f:
|
|
|
|
last_restart = int(f.read().strip())
|
|
|
|
except:
|
|
|
|
last_restart = 0
|
|
|
|
|
|
|
|
if now-3600 < last_restart:
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
ifaces = set()
|
|
|
|
for i in netifaces.interfaces():
|
|
|
|
if i.startswith('ppp'):
|
|
|
|
ifaces.add(i)
|
|
|
|
|
|
|
|
system_has_public_ip = False
|
|
|
|
for iface in ifaces:
|
|
|
|
for type in [netifaces.AF_INET, netifaces.AF_INET6]:
|
2021-04-10 07:38:47 +00:00
|
|
|
try:
|
|
|
|
ips = netifaces.ifaddresses(iface)[type]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for ip in ips
|
2020-12-10 15:14:17 +00:00
|
|
|
try:
|
|
|
|
addr = ip_address(ip['addr'])
|
|
|
|
|
|
|
|
if not addr.is_private and not addr.is_loopback:
|
|
|
|
system_has_public_ip = True
|
2021-04-10 07:38:47 +00:00
|
|
|
except Exception:
|
2020-12-10 15:14:17 +00:00
|
|
|
# Apparently not an ip
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not system_has_public_ip:
|
|
|
|
run(['systemctl', 'restart', 'pppoe'])
|
|
|
|
|
|
|
|
with open('/var/tmp/pppd-last-restart.status', 'w') as f:
|
2020-12-18 05:32:18 +00:00
|
|
|
f.write(str(now))
|
2020-12-10 15:14:17 +00:00
|
|
|
|
|
|
|
print('pppoe.service has been restarted')
|