cfg/.config/i3blocks/vpn
Knut Ahlers e95471951a
Fix: PyYAML wants a specified loader
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-09-11 19:54:25 +02:00

48 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python
from pathlib import Path
import subprocess
import sys
import yaml
def check_ping_reachable(ip):
try:
subprocess.check_call(['ping', '-c2', '-W1', ip],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return True
except subprocess.CalledProcessError:
return False
def check_service_active(service):
try:
subprocess.check_call(['systemctl', 'status', service],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return True
except subprocess.CalledProcessError:
return False
def main():
config = yaml.load(
open(Path('~/.config/i3-vpn.json').expanduser()),
Loader=yaml.SafeLoader,
)
results = [config['prefix']]
for check in config['checks']:
if check_ping_reachable(check['ip']):
check['color'] = config['colors']['success']
else:
check['color'] = config['colors']['failure']
results.append(config['template'].format(**check))
print(' '.join(results))
if __name__ == '__main__':
sys.exit(main())