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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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.