Wei-Yu Chen | 84cbc36 | 2022-01-11 23:31:27 +0800 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | """ |
| 3 | cbrs_backup.py |
| 4 | |
| 5 | a tool to download log file from target eNB devices |
| 6 | """ |
| 7 | |
| 8 | # SPDX-FileCopyrightText: © 2022 Open Networking Foundation <support@opennetworking.org> |
| 9 | # SPDX-License-Identifier: Apache-2.0 |
| 10 | |
| 11 | import argparse |
| 12 | import base64 |
| 13 | import datetime |
| 14 | import requests |
| 15 | from lxml import etree |
| 16 | |
| 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument("address", help="eNB IP address", type=str) |
| 19 | parser.add_argument( |
Zack Williams | 84cb77a | 2022-01-14 14:46:39 -0800 | [diff] [blame] | 20 | "-u", "--username", help="eNB UI login username", type=str, default="sc_femto" |
Wei-Yu Chen | 84cbc36 | 2022-01-11 23:31:27 +0800 | [diff] [blame] | 21 | ) |
| 22 | parser.add_argument( |
Zack Williams | 84cb77a | 2022-01-14 14:46:39 -0800 | [diff] [blame] | 23 | "-p", "--password", help="eNB UI login password", type=str, default="scHt3pp" |
Wei-Yu Chen | 84cbc36 | 2022-01-11 23:31:27 +0800 | [diff] [blame] | 24 | ) |
| 25 | args = parser.parse_args() |
| 26 | |
| 27 | ENB_DOMAIN = f"https://{args.address}/" |
| 28 | CGI_URL = ENB_DOMAIN + "setup.cgi" |
| 29 | |
Zack Williams | 84cb77a | 2022-01-14 14:46:39 -0800 | [diff] [blame] | 30 | USERNAME = bytes(args.username.encode()) |
| 31 | PASSWORD = bytes(args.password.encode()) |
Wei-Yu Chen | 84cbc36 | 2022-01-11 23:31:27 +0800 | [diff] [blame] | 32 | |
| 33 | # Disable SSL verification since we signed the certificate by our own |
| 34 | session = requests.session() |
| 35 | session.verify = False |
| 36 | |
| 37 | # The login payload passed to cgi gateway |
| 38 | login_payload = { |
| 39 | "un": base64.b64encode(USERNAME).decode(), |
| 40 | "pw": base64.b64encode(PASSWORD).decode(), |
| 41 | "this_file": "logon.htm", |
| 42 | "next_file": "status.htm", |
| 43 | "todo": "login", |
| 44 | } |
| 45 | |
| 46 | r = session.post(CGI_URL, data=login_payload) |
| 47 | e = etree.HTML(r.text) |
| 48 | |
| 49 | serial_number = e.xpath("//td")[4].text |
| 50 | |
| 51 | download_payload = { |
| 52 | "this_file": "logsave.htm", |
| 53 | "next_file": "logsave.htm", |
| 54 | "todo": "logdownLoad", |
| 55 | } |
| 56 | |
| 57 | with session.post(CGI_URL, data=download_payload, stream=True) as r: |
| 58 | r.raise_for_status() |
| 59 | timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 60 | filename = f"{serial_number}_{timestamp}_binary.zip" |
| 61 | |
| 62 | with open(filename, "wb") as f: |
| 63 | for chunk in r.iter_content(chunk_size=8192): |
| 64 | f.write(chunk) |
| 65 | |
| 66 | print(f"{filename} log backup at {timestamp}.") |
| 67 | |
| 68 | |
| 69 | text_download_payload = { |
| 70 | "this_file": "logsave.htm", |
| 71 | "next_file": "logsave.htm", |
| 72 | "todo": "TxtlogdownLoad", |
| 73 | } |
| 74 | |
| 75 | with session.post(CGI_URL, data=text_download_payload, stream=True) as r: |
| 76 | r.raise_for_status() |
| 77 | timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 78 | filename = f"{serial_number}_{timestamp}_text.zip" |
| 79 | |
| 80 | with open(filename, "wb") as f: |
| 81 | for chunk in r.iter_content(chunk_size=8192): |
| 82 | f.write(chunk) |
| 83 | |
| 84 | print(f"{filename} log backup at {timestamp}.") |
Wei-Yu Chen | cb75efa | 2022-01-14 02:00:12 +0800 | [diff] [blame] | 85 | |
| 86 | |
| 87 | clean_payload = { |
| 88 | "this_file": "logsave.htm", |
| 89 | "next_file": "logsave.htm", |
| 90 | "todo": "logClear", |
| 91 | } |
| 92 | |
| 93 | r = session.post(CGI_URL, data=clean_payload) |
| 94 | print(f"{serial_number} log cleared.") |