Backup the Cisco Expressway Using Python


This Cisco Expressway can be backed up with a backup encryption password relatively easy using some simple Python code.

The script requires updating the following variables before running.

  • URL
  • PASSWORD
  • BACKUP_PASSWORD

The script will save the Expressway backup file to the directory where it is run. Also available as a GitHub Gist here.

import requests
import json
import re


def main():
    URL = "https://10.1.1.1"  # UPDATE
    USERNAME = "admin"
    PASSWORD = "password"  # UPDATE
    BACKUP_PASSWORD = "password"  # UPDATE
    sessionid = ""

    # Setup Session
    s = requests.Session()

    # Login
    r = s.post(
        url=f"{URL}/login",
        data={
            "submitButton": "Login",
            "username": USERNAME,
            "password": PASSWORD,
            "formbutton": "Login",
        },
        headers={"Referer": f"{URL}/login"},
        verify=False,
    )

    # Load Backup and restore page
    r = s.get(url=f"{URL}/backuprestore", verify=False)

    # Get the sessionid value
    # Split the response lines
    for line in r.text.splitlines():
        if re.search(string=line, pattern="sessionid"):
            line = line.split("/><")
            line = line[0].split("><")
            line = re.sub(string=line[1], pattern='"', repl="")
            sessionid = line.split("value=")[1]
            break

    data = f"sessionid={sessionid}&submitbutton=Create+system+backup+file&backup_password={BACKUP_PASSWORD}&backup_password_confirm={BACKUP_PASSWORD}"
    headers = {"Content-Type": "application/x-www-form-urlencoded"}

    with s.post(
        url=f"{URL}/backuprestore",
        data=data,
        verify=False,
        headers=headers,
        stream=True,
    ) as r:
        r.raise_for_status()

        timestamp = datetime.utcnow().strftime("%Y_%m_%d__%H_%M_%S")
        filename = f"expressway_{timestamp}_backup.tar.gz.enc"

        with open(filename, "wb") as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)


if __name__ == "__main__":
    main()

Note, I've also added the same backup functionality to my py-expressway Python module here.