From 366263e17ff0efc2a5c6a6def068cae603cbe1ba Mon Sep 17 00:00:00 2001 From: JLKwong <61807745+JLKwong@users.noreply.github.com> Date: Wed, 31 Mar 2021 20:33:04 -0700 Subject: [PATCH] `treehouses internet reverse` refactor (fixes #2137) (#2140) Co-authored-by: dogi --- modules/internet.sh | 24 +++++++++++++++++------- modules/remote.sh | 12 ++++++------ package.json | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/modules/internet.sh b/modules/internet.sh index d8295d5e60..d6b7351ff9 100644 --- a/modules/internet.sh +++ b/modules/internet.sh @@ -8,19 +8,29 @@ function internet { exit 0 fi echo "false" - ;; + ;; "reverse") if ! nc -w 10 -z 8.8.8.8 53 >/dev/null 1>&2; then log_and_exit1 "Error: no internet found" fi info="$(curl -s ipinfo.io | grep -o '"[^"]*"\s*:\s*"[^"]*"')" - echo "$info" | grep -E '"(ip)"' - if ! echo "$info" | grep -E 'postal'; then - echo "$info" | grep -E '"(city|country)"' | tr '\n' ',' | sed 's/,$/, "postal": "n\/a"\n/' | sed 's/\",\"/\", \"/g' - else - echo "$info" | grep -E '"(city|country|postal)"' | tr '\n' ',' | sed 's/,$/\n/' | sed 's/\",\"/\", \"/g' + ip=$(echo "$info" | grep -e '"ip": "') + org=$(echo "$info" | grep -e '"org": "') + country=$(echo "$info" | grep -o '"country": "[^;]*') + city=$(echo $info | grep -o '"city": "[^;]*' | cut -d '"' -f 4) + postal=$(echo $info | grep -o '"postal": "[^;]*' | cut -d '"' -f 4) + timezone=$(echo $info | grep -o '"timezone": "[^;]*' | cut -d '"' -f 4) + + if [ -z "$postal" ]; then + postal="n/a" fi - echo "$info" | grep -E '"(org|timezone)"' + + echo "$ip" + echo "$org" + echo "$country" + echo "\"city\": \"$city\"" + echo "\"postal\": \"$postal\"" + echo "\"timezone\": \"$timezone\"" ;; *) log_help_and_exit1 "ERROR: incorrect command" internet diff --git a/modules/remote.sh b/modules/remote.sh index 6d3a2a2f69..08cc0d46e2 100644 --- a/modules/remote.sh +++ b/modules/remote.sh @@ -76,10 +76,10 @@ function remote { cmd_str+="\"$line\"," done <<< "$reverse" ip=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 1) - org=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 5) - country=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 4) - city=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 3) - postal=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 2) + org=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 2) + country=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 3) + city=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 4) + postal=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 5) timezone=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 6) echo "{$ip,$org,$country,$city,$postal,$timezone}" @@ -120,13 +120,13 @@ function remote { showuser=$(ssh 2fa show $user) if [[ "$showuser" == "SSH 2FA for $user is disabled." ]]; then outputpart="\"$user\":\"disabled\"," - else + else secret="$(echo "$showuser" | head -n 1 | sed 's/Secret Key://g' | sed -r 's/\s+//g')" scratch="$(echo "$showuser" | awk 'NR>3' | sed 's/.*/"&"/' | awk '{printf "%s"",",$0}' | sed 's/,$//')" outputpart="\"$user\":{\"secret key\":\"$secret\",\"scratch codes\":[$scratch]}," fi output="$output$outputpart" - done + done echo "{${output::-1}}" ;; "help") diff --git a/package.json b/package.json index 20f1713e8c..25675936ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.25.36", + "version": "1.25.37", "remote": "4000", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh",