Skip to content

Commit 366263e

Browse files
JLKwongdogi
andauthored
treehouses internet reverse refactor (fixes #2137) (#2140)
Co-authored-by: dogi <dogi@users.noreply.github.com>
1 parent e73b306 commit 366263e

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

modules/internet.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@ function internet {
88
exit 0
99
fi
1010
echo "false"
11-
;;
11+
;;
1212
"reverse")
1313
if ! nc -w 10 -z 8.8.8.8 53 >/dev/null 1>&2; then
1414
log_and_exit1 "Error: no internet found"
1515
fi
1616
info="$(curl -s ipinfo.io | grep -o '"[^"]*"\s*:\s*"[^"]*"')"
17-
echo "$info" | grep -E '"(ip)"'
18-
if ! echo "$info" | grep -E 'postal'; then
19-
echo "$info" | grep -E '"(city|country)"' | tr '\n' ',' | sed 's/,$/, "postal": "n\/a"\n/' | sed 's/\",\"/\", \"/g'
20-
else
21-
echo "$info" | grep -E '"(city|country|postal)"' | tr '\n' ',' | sed 's/,$/\n/' | sed 's/\",\"/\", \"/g'
17+
ip=$(echo "$info" | grep -e '"ip": "')
18+
org=$(echo "$info" | grep -e '"org": "')
19+
country=$(echo "$info" | grep -o '"country": "[^;]*')
20+
city=$(echo $info | grep -o '"city": "[^;]*' | cut -d '"' -f 4)
21+
postal=$(echo $info | grep -o '"postal": "[^;]*' | cut -d '"' -f 4)
22+
timezone=$(echo $info | grep -o '"timezone": "[^;]*' | cut -d '"' -f 4)
23+
24+
if [ -z "$postal" ]; then
25+
postal="n/a"
2226
fi
23-
echo "$info" | grep -E '"(org|timezone)"'
27+
28+
echo "$ip"
29+
echo "$org"
30+
echo "$country"
31+
echo "\"city\": \"$city\""
32+
echo "\"postal\": \"$postal\""
33+
echo "\"timezone\": \"$timezone\""
2434
;;
2535
*)
2636
log_help_and_exit1 "ERROR: incorrect command" internet

modules/remote.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ function remote {
7676
cmd_str+="\"$line\","
7777
done <<< "$reverse"
7878
ip=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 1)
79-
org=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 5)
80-
country=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 4)
81-
city=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 3)
82-
postal=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 2)
79+
org=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 2)
80+
country=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 3)
81+
city=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 4)
82+
postal=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 5)
8383
timezone=$(printf "%s\n" "${cmd_str::-1}" | cut -d',' -f 6)
8484

8585
echo "{$ip,$org,$country,$city,$postal,$timezone}"
@@ -120,13 +120,13 @@ function remote {
120120
showuser=$(ssh 2fa show $user)
121121
if [[ "$showuser" == "SSH 2FA for $user is disabled." ]]; then
122122
outputpart="\"$user\":\"disabled\","
123-
else
123+
else
124124
secret="$(echo "$showuser" | head -n 1 | sed 's/Secret Key://g' | sed -r 's/\s+//g')"
125125
scratch="$(echo "$showuser" | awk 'NR>3' | sed 's/.*/"&"/' | awk '{printf "%s"",",$0}' | sed 's/,$//')"
126126
outputpart="\"$user\":{\"secret key\":\"$secret\",\"scratch codes\":[$scratch]},"
127127
fi
128128
output="$output$outputpart"
129-
done
129+
done
130130
echo "{${output::-1}}"
131131
;;
132132
"help")

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@treehouses/cli",
3-
"version": "1.25.36",
3+
"version": "1.25.37",
44
"remote": "4000",
55
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
66
"main": "cli.sh",

0 commit comments

Comments
 (0)