Location and Address Automatically

Location and Address Automatically

Introduction:
Often when the Nodegrid comes up. it would be great if the NG knows where it is automatically. currently, the user would have to enter the address and the coordinates manually in the System::Preferences.

Requirement:
Warning
The Nodegrid needs to be plugged into an ethernet IP network with internet accessible

Steps:
This script will automatically run when the Nodegrid boots up.

1. In the Nodegrid console log in as root "shell sudo su -"
2. Copy the attached script "getNGLoc.sh" to the Nodegrid /etc/init.d folder
3. Go to /etc/init.d folder and change script to be executable "chmod +x getNGLoc.sh"
4. Go to /etc/rc5.d folder
5. Set a soft link to the script "ln -s /etc/init.d/getNGLoc.sh S99getNGLoc.sh"

The system will now run this script when the Nodegrid boots up to update the System::Preference address and coordinates

getNGLoc.sh
  1. #!/bin/bash
  2. # wait for the system to comes up first before execution sleep 60 Seconds
  3. sleep 60
  4. myLocData=$(curl http://ip-api.com/json?fields=lat,lon)
  5. longitude=$(echo "$myLocData" | jq -r .lon)
  6. latitude=$(echo "$myLocData" | jq -r .lat)
  7. coord="$latitude,$longitude"

  8. # Fetch geolocation data using ipinfo.io
  9. location_data=$(curl -s ipinfo.io)
  10. # Extract city and region (state) from the JSON response
  11. city=$(echo $location_data | jq -r '.city')
  12. state=$(echo $location_data | jq -r '.region')
  13. address="$city,$state"

  14. tmpFile="/home/admin/outLoc.cli"
  15. echo "cd /settings/system_preferences/" > $tmpFile
  16. echo "set address_location=$address" >> $tmpFile
  17. echo "set coordinates=$coord" >> $tmpFile
  18. echo "commit" >> $tmpFile
  19. su - admin -c "cli -f $tmpFile"
  20. rm $tmpFile