From 406abcbcc395f077645b593ae8b6bd12b650e429 Mon Sep 17 00:00:00 2001
From: Nick Hainke <vincent@systemli.org>
Date: Sun, 3 Apr 2022 12:03:52 +0200
Subject: [PATCH] ubnt-manager: add ubnt-manager

This app makes it easier to work with AirOS devices. So far, only monitoring is implemented.

Signed-off-by: Nick Hainke <vincent@systemli.org>
(cherry picked from commit e4a8d3fc29d8f7f00089b14564d5248694e12638)
---
 utils/ubnt-manager/Makefile                  | 40 ++++++++++
 utils/ubnt-manager/files/ubnt-manager.config |  9 +++
 utils/ubnt-manager/files/ubnt-manager.sh     | 79 ++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 utils/ubnt-manager/Makefile
 create mode 100644 utils/ubnt-manager/files/ubnt-manager.config
 create mode 100755 utils/ubnt-manager/files/ubnt-manager.sh

diff --git a/utils/ubnt-manager/Makefile b/utils/ubnt-manager/Makefile
new file mode 100644
index 0000000000..8416eff13d
--- /dev/null
+++ b/utils/ubnt-manager/Makefile
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=ubnt-manager
+PKG_VERSION:=1
+PKG_RELEASE:=$(AUTORELEASE)
+
+PKG_MAINTAINER:=Nick Hainke <vincent@systemli.org>
+PKG_LICENSE:=GPL-2.0-only
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/ubnt-manager
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=Managment app for Ubiquiti devices
+  PKGARCH:=all
+  EXTRA_DEPENDS:=dropbear
+endef
+
+define Package/ubnt-manager/description
+Managment app for Ubiquiti devices.
+endef
+
+define Package/ubnt-manager/conffiles
+/etc/config/ubnt-manager
+endef
+
+define Build/Compile
+endef
+
+define Package/ubnt-manager/install
+	$(INSTALL_DIR) $(1)/usr/bin
+	$(INSTALL_BIN) ./files/ubnt-manager.sh $(1)/usr/bin/ubnt-manager
+	$(INSTALL_DIR) $(1)/etc/config
+	$(INSTALL_DATA) ./files/ubnt-manager.config $(1)/etc/config/ubnt-manager
+endef
+
+$(eval $(call BuildPackage,ubnt-manager))
diff --git a/utils/ubnt-manager/files/ubnt-manager.config b/utils/ubnt-manager/files/ubnt-manager.config
new file mode 100644
index 0000000000..bc29cdf063
--- /dev/null
+++ b/utils/ubnt-manager/files/ubnt-manager.config
@@ -0,0 +1,9 @@
+config device 'sample_ap' # make sure to not use dashes in name
+	option target		'192.168.1.20'
+	option username		'ubnt'
+	option password		'ubnt'
+
+#config device 'sample_ap1'
+#	option target		'10.31.81.21'
+#	option username		'ubnt'
+#	option password		'...'
diff --git a/utils/ubnt-manager/files/ubnt-manager.sh b/utils/ubnt-manager/files/ubnt-manager.sh
new file mode 100755
index 0000000000..c65eb2363a
--- /dev/null
+++ b/utils/ubnt-manager/files/ubnt-manager.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+. /lib/functions.sh
+
+log() {
+    local msg="$1"
+    logger -t ubnt-manager -s "$msg"
+}
+
+rexec() {
+    local target="$1"
+    local username="$2"
+    local password="$3"
+    local cmd="$4"
+    raw=$(DROPBEAR_PASSWORD="$password" ssh -y $username@$target "$cmd" 2>/dev/null)
+    ssh_result=$?
+}
+
+get_json_dump() {
+    local cmd="/usr/www/status.cgi"
+    rexec $* "$cmd"
+    echo $raw
+}
+
+handle_device() {
+    local device="${1//-/_}" # replace "-" with "_"
+    config_load ubnt-manager
+    config_get target "$device" target
+    config_get username "$device" username
+    config_get password "$device" password
+    ssh_result=0
+}
+
+add_device_to_list() {
+    local device="$1"
+    device_list="$device_list $device"
+}
+
+list_devices() {
+    device_list=""
+    config_load ubnt-manager
+    config_foreach add_device_to_list device device_list
+    echo $device_list
+}
+
+usage() {
+    cat <<EOF
+usage: ubnt-manager [command]
+-j    | --json          Dump json info
+-t    | --target        Target device
+-l    | --list-devices  List all devices
+-h    | --help          Brings up this menu
+EOF
+}
+
+while [ "$1" != "" ]; do
+    case $1 in
+    -t | --target)
+        shift
+        target=$1
+        handle_device $target
+        ;;
+    -j | --json)
+        json=1
+        ;;
+    -l | --list-devices)
+        list_devices
+        ;;
+    -h | --help)
+        usage
+        ;;
+    esac
+    shift
+done
+
+if [ ! -z $json ]; then
+    get_json_dump $target $username $password | sed 's/Content-Type:\ application\/json//'
+fi
-- 
2.30.2