main
59c7d64 · 8 days ago 139 commits
 1#!/usr/bin/env zsh
 2
 3set -euo pipefail
 4
 5PASSWORD=$(zenity --password --title="Router Login")
 6
 7JSON=$(tplinkctl -password "$PASSWORD" "/admin/status?form=all" | jq ".data.access_devices_wired + .data.access_devices_wireless_host")
 8copy() {
 9    if command -v wl-copy &>/dev/null; then
10        printf '%s' "$1" | wl-copy
11    else
12        printf '%s' "$1" | xclip -selection clipboard
13    fi
14}
15
16main_menu() {
17    echo "$JSON" | jq -r '.[] | "\(.hostname)\t\(.ipaddr)\t\(.macaddr)\t[\(.wire_type)]"'\
18        | column -t -s $'\t' \
19        | rofi -dmenu -i -p " Devices:" -theme-str 'window {width: 37%;}' -theme-str 'listview {lines:5;}'
20}
21
22device_menu() {
23    local hostname="$1" ip="$2" mac="$3"
24    local action
25    action=$(printf "Copy IP: %s\nCopy MAC: %s\n← Back" "$ip" "$mac" \
26			| rofi -dmenu -i -theme "$HOME/.wraith/theme/scripts.rofi.rasi" -theme-str 'listview {lines:3;}')
27
28    case "$action" in
29        Copy\ IP*)   copy "$ip";  notify-send "Copied IP"  "$ip" ;;
30        Copy\ MAC*)  copy "$mac"; notify-send "Copied MAC" "$mac" ;;
31        "← Back")    run ;;
32        *)           exit 0 ;;
33    esac
34}
35
36run() {
37    local choice hostname ip mac
38    choice=$(main_menu) || exit 0
39    [[ -z "$choice" ]] && exit 0
40
41    hostname=$(awk '{print $1}' <<< "$choice")
42    ip=$(echo "$JSON" | jq -r --arg h "$hostname" '.[] | select(.hostname==$h) | .ipaddr')
43    mac=$(echo "$JSON" | jq -r --arg h "$hostname" '.[] | select(.hostname==$h) | .macaddr')
44
45    device_menu "$hostname" "$ip" "$mac"
46}
47
48run