diff --git a/modules/internet.sh b/modules/internet.sh index d8295d5e6..d6b7351ff 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 97fa46511..3e44d0ede 100644 --- a/modules/remote.sh +++ b/modules/remote.sh @@ -71,14 +71,18 @@ function remote { ;; "reverse") checkargn $# 2 - reverse=$(internet reverse | cut -d'"' -f 2,3,4 | sed 's#\:[[:space:]]\"#:\"#g') - ip=$(echo "$reverse" | grep 'ip":"') - org=$(echo "$reverse" | grep 'org":"') - country=$(echo "$reverse" | grep 'country":"') - city=$(echo "$reverse" | grep 'city":"') - postal=$(echo "$reverse" | grep 'postal":"') - timezone=$(echo "$reverse" | grep 'timezone":"') - echo "{$ip\",\"$org\",\"$country\",\"$city\",\"$postal\",\"$timezone}" + reverse=$(internet reverse | sed -e 's#",\ "#"\n"#g' | cut -d'"' -f 2,3,4 | sed 's#\:[[:space:]]\"#:\"#g') + while IFS= read -r line; do + 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 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}" ;; "allservices") checkargn $# 1 diff --git a/package.json b/package.json index 20f1713e8..25675936a 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",