Created
October 17, 2018 17:01
-
-
Save Demonslay335/bfbc9acb93632a10611fee27e19a034e to your computer and use it in GitHub Desktop.
Get status of IPsec VPN tunnels on Peplink Balance
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 requests, time | |
from urllib3.exceptions import InsecureRequestWarning | |
from xml.etree import ElementTree | |
# Ignore self-signed SSL | |
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) | |
# Build login payload | |
payload = { | |
'username': 'admin', | |
'password': 'mypassword', | |
'func': 'login' | |
} | |
# IP of PepLink Balance device | |
# Tested on Balance 580 series w/ firmware 7.0.2+ | |
ip = '192.168.1.1' | |
# Current time | |
t = int(time.time()) | |
# URLs for login and IPsec status | |
login_url = 'https://%s/cgi-bin/MANGA/api.cgi' % ip | |
ipsec_url = 'https://%s/cgi-bin/MANGA/data.cgi?option=ipsec_status&_=%d' % (ip, t) | |
# Start session to save cookies | |
session = requests.session() | |
# Submit login request | |
login_response = session.post(login_url, data=payload, verify=False) | |
# Get as JSON | |
data = login_response.json() | |
# Check for successful login | |
if(data['stat'] != 'ok'): | |
print 'Failed login: %s' % data['message'] | |
exit() | |
# Request IPsec status | |
ipsec_response = session.get(ipsec_url, verify=False) | |
# Parse XML | |
ipsec_tree = ElementTree.fromstring(ipsec_response.content) | |
# Iterate the profiles | |
for ipsec_profile in ipsec_tree[1].iter('ipsec_profile'): | |
# Output ID and tunnel name | |
print "ID: %s\nName: %s" % (ipsec_profile.get('id'), ipsec_profile.find('name').text) | |
# Iterate the paths | |
for ipsec_path in ipsec_profile.iter('ipsec_path'): | |
# Output local and remote IPs | |
print "Local: %s\nRemote: %s" % (ipsec_path.find('local').text, ipsec_path.find('remote').text) | |
# Output whether path is active | |
print "Active: %s" % ('Yes' if ipsec_path.find('active') is not None else 'No') | |
print "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment