Skip to content

Instantly share code, notes, and snippets.

@jneight
Created October 21, 2017 15:35
Show Gist options
  • Save jneight/82600aeeb9e0a79d570a0b734dc25072 to your computer and use it in GitHub Desktop.
Save jneight/82600aeeb9e0a79d570a0b734dc25072 to your computer and use it in GitHub Desktop.
Check ipsec VPN status
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