Add cbrs_backup.py for downloading enb log

AETHER-2939

Change-Id: Ia95710d23a6db944d47ed4f033d28e81e68daad0
diff --git a/cbrs_backup.py b/cbrs_backup.py
new file mode 100644
index 0000000..69a08bc
--- /dev/null
+++ b/cbrs_backup.py
@@ -0,0 +1,84 @@
+#! /usr/bin/python
+"""
+cbrs_backup.py
+
+a tool to download log file from target eNB devices
+"""
+
+# SPDX-FileCopyrightText: © 2022 Open Networking Foundation <support@opennetworking.org>
+# SPDX-License-Identifier: Apache-2.0
+
+import argparse
+import base64
+import datetime
+import requests
+from lxml import etree
+
+parser = argparse.ArgumentParser()
+parser.add_argument("address", help="eNB IP address", type=str)
+parser.add_argument(
+    "-u", "--username", help="eNB UI login username", type=bytes, default=b"sc_femto"
+)
+parser.add_argument(
+    "-p", "--password", help="eNB UI login password", type=bytes, default=b"scHt3pp"
+)
+args = parser.parse_args()
+
+ENB_DOMAIN = f"https://{args.address}/"
+CGI_URL = ENB_DOMAIN + "setup.cgi"
+
+USERNAME = args.username
+PASSWORD = args.password
+
+# Disable SSL verification since we signed the certificate by our own
+session = requests.session()
+session.verify = False
+
+# The login payload passed to cgi gateway
+login_payload = {
+    "un": base64.b64encode(USERNAME).decode(),
+    "pw": base64.b64encode(PASSWORD).decode(),
+    "this_file": "logon.htm",
+    "next_file": "status.htm",
+    "todo": "login",
+}
+
+r = session.post(CGI_URL, data=login_payload)
+e = etree.HTML(r.text)
+
+serial_number = e.xpath("//td")[4].text
+
+download_payload = {
+    "this_file": "logsave.htm",
+    "next_file": "logsave.htm",
+    "todo": "logdownLoad",
+}
+
+with session.post(CGI_URL, data=download_payload, stream=True) as r:
+    r.raise_for_status()
+    timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
+    filename = f"{serial_number}_{timestamp}_binary.zip"
+
+    with open(filename, "wb") as f:
+        for chunk in r.iter_content(chunk_size=8192):
+            f.write(chunk)
+
+    print(f"{filename} log backup at {timestamp}.")
+
+
+text_download_payload = {
+    "this_file": "logsave.htm",
+    "next_file": "logsave.htm",
+    "todo": "TxtlogdownLoad",
+}
+
+with session.post(CGI_URL, data=text_download_payload, stream=True) as r:
+    r.raise_for_status()
+    timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
+    filename = f"{serial_number}_{timestamp}_text.zip"
+
+    with open(filename, "wb") as f:
+        for chunk in r.iter_content(chunk_size=8192):
+            f.write(chunk)
+
+    print(f"{filename} log backup at {timestamp}.")
diff --git a/requirements.txt b/requirements.txt
index 5432d5d..3b5f73e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,4 @@
 python-jose~=3.3.0
 pylint~=2.7.2
 reuse~=0.11.1
+lxml~=4.6.4