Skip to content

Commit

Permalink
treehouses internet reverse refactor (fixes #2137) (#2140)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
JLKwong and dogi authored Apr 1, 2021
1 parent e73b306 commit 366263e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
24 changes: 17 additions & 7 deletions modules/internet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions modules/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 366263e

Please sign in to comment.