Created
October 21, 2017 15:35
-
-
Save jneight/82600aeeb9e0a79d570a0b734dc25072 to your computer and use it in GitHub Desktop.
Check ipsec VPN status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import subprocess | |
def get_vpn_names(path): | |
with open(path, 'r') as f: | |
return re.findall(r'conn ([\w]+)', read) | |
def check_vpn(vpn_name): | |
result = subprocess.check_output(['ipsec', 'status', vpn_name]) | |
if 'ESTABLISHED' in result: | |
return True | |
return False | |
def up_vpn(vpn_name): | |
result = subprocess.check_output(['ipsec', 'up', vpn_name]) | |
if 'connection \'{}\' established successfully'.format(vpn_name) in result: | |
return True | |
return False | |
if __name__ == '__main__': | |
vpns_to_check = get_vpn_names('/etc/ipsec.conf') | |
for vpn in vpns_to_check: | |
if check_vpn(vpn): | |
up_vpn(vpn) | |
return len(vpns_to_check) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment