+++ /dev/null
-#!/usr/bin/python3
-#
-# Back up customer Untangle boxes over HTTPS.
-#
-
-import configparser
-import http.cookiejar
-import ssl
-import sys
-import urllib.parse
-import urllib.request
-
-
-class Untangle:
-
- def __init__(self, s):
- """
- Initialize this Untangle object with a ConfigParser section.
- """
- self.name = s.name
- self.host = s['host']
- self.username = s['username']
- self.password = s['password']
- self.version = int(s['version'])
-
- # Create an opener object that we'll use to make HTTP requests.
- cj = http.cookiejar.CookieJar()
-
- # SSL mumbo jumbo to make it ignore the certificate's hostname.
- ssl_ctx = ssl._create_unverified_context()
- https_handler = urllib.request.HTTPSHandler(context=ssl_ctx)
-
- # Tell it to use our cookie jar.
- cookie_proc = urllib.request.HTTPCookieProcessor(cj)
- self.opener = urllib.request.build_opener(https_handler, cookie_proc)
-
- # Also set the base URL which will never change.
- self.base_url = 'https://' + self.host + '/'
-
-
- def login(self):
- login_path = 'auth/login?url=/setup/welcome.do&realm=Administrator'
- url = self.base_url + login_path
- post_vars = {'username': self.username, 'password': self.password }
- post_data = urllib.parse.urlencode(post_vars).encode('ascii')
- self.opener.open(url, post_data)
-
-
- def get_backup(self):
- if self.version == 9:
- return self.get_backup_v9()
- elif self.version == 11:
- return self.get_backup_v11()
-
-
- def get_backup_v9(self):
- url = self.base_url + '/webui/backup'
- post_vars = {'action': 'requestBackup'}
- post_data = urllib.parse.urlencode(post_vars).encode('ascii')
- self.opener.open(url, post_data)
-
- url = self.base_url + 'webui/backup?action=initiateDownload'
- with self.opener.open(url) as response:
- return response.read()
-
-
- def get_backup_v11(self):
- url = self.base_url + '/webui/download?type=backup'
- post_vars = {'type': 'backup'}
- post_data = urllib.parse.urlencode(post_vars).encode('ascii')
- with self.opener.open(url, post_data) as response:
- return response.read()
-
-
-
-config = configparser.ConfigParser()
-config.read('untangle-backup.ini')
-
-for section in config.sections():
- untangle = Untangle(config[section])
- try:
- untangle.login()
- backup = untangle.get_backup()
- filename = untangle.name + '.backup'
- with open(filename, 'wb') as f:
- f.write(backup)
- except urllib.error.URLError as e:
- msg = 'Connection error (' + str(e.reason) + ')'
- msg += ' from ' + untangle.host + '.'
- print(msg, file=sys.stderr)
- except urllib.error.HTTPError as e:
- msg = 'HTTP error ' + str(e.code) + ' from ' + untangle.host + '.'
- print(msg, file=sys.stderr)