IGEL OS automatic Time Zone sync based on geo location detection. Example of how IGEL OS scripting can help!

By Fredrik Brattstig @virtualbrat

As a frequent traveler, often traveling for business purposes from Sweden to different locations in the United States, I’m always facing the manual tasks och correcting the Time Zone settings on my IGEL OS endpoint when I arrive at my destination. As I wrote in the previous blog here, IGEL now added the possibility for the user to alter the Time Zone using the system tray tool on IGEL OS 12. Thats great! But I want it to happen automatically, as I am lazy 🙂
Automating the Time Zone detection and click adjustment becomes even more powerful when living and traveling within a country with multiple Time Zones, like the United States as example.
When thinking about it, one of the superhero perks in IGEL OS is the scripting possibilities, and that does not only apply to the local operating system. You can use the IGEL Universal Management Suite (UMS) to deploy scripts to any or all of your endpoints in your organization. Build the script, test it and then deploy simply anywhere! That’s one of the hidden gems in IGEL OS and the UMS – The power of scripts!

As said, I want to fulfill my laziness, and have my IGEL operating system to automatically detect its current time zone as soon as possible when I’m connecting to a network in a foreign Time Zone. I will use my UMS to create a profile that runs a script on my IGEL OS endpoints every time my network connection is restored.

First of all, how and when do I want the script to run? As an experienced IGEL OS admin, I know that I can use System Customization – Custom Commands to run commands in different stages of the startup of the operating system. There is also a nifty feature to run commands based on the network connectivity. In my case I will run the script when the network connection has been restored.

OK, if I put my script in “Final Network Command”, it will run every time my network connection is restored. Check!

The script would first need to find my geographic location. I will use the public IP-address when I’m accessing the internet for IGEL OS to determine where I am. I found this useful website: https://ipapi.co that gives me this information when browsed:


We see the Time Zone, and the Latitude and Longitude, these values are the data we need for IGEL OS to be able to determine the geo location, as a foundation for setting the Time Zone and set the clock correct. Though, the website displayed above by the browser view makes it hard to capture the values we need for a script. so, now what?
The command line tool curl can talk to ipapi.co and retrieve the data in Json format, how does that look?

Nice, the Json response includes Time Zone, Lattitude and Longitude. Maybe we can make this even easier? Using the command curl -s https://ipapi.co/latlong returns 58.380700,12.323400 and curl -s https://ipapi.co/timezone returns Europe/Stockholm. Awesome! Now we have something to work with!
Putting this to practice means that we have to
1. Retrieve Latitude and Longitude
2. Separate Latitude and Longitude
3. Retrieve Time Zone
4. Set the Time Zone
5. Sync the clock based on the Latitude and Longitude

#!/bin/bash
  
# Get the geolocation
LAT_LNG=$(curl -s https://ipapi.co/latlong/)
LAT=$(echo $LAT_LNG | cut -d ',' -f 1)
LNG=$(echo $LAT_LNG | cut -d ',' -f 2)

# Get TimeZone
TZ=$(curl -s https://ipapi.co/timezone/)

# Set the timezone
timedatectl set-timezone $TZ

# Sync the clock
ntpdate $LAT $LNG pool.ntp.org

Thats it!
In the first section we declare to use a bash script. 2nd section, we retrieve the latitude and longitude based on my external IP as one string, then we separate the latitude and assign it to the variable LAT, and separate longitude and assign it to the variable LNG. In the 3rd section we retrieve the Time Zone based on my external IP. Then, in the 4th section we set the Time Zone for the operating system and finally in the 5th section we update the time in the operating system based on the latitude and longitude with a NTP server (I’ve chosen to use pool.ntp.org).

Now, let’s add the script to the UMS profile:


Actually, I tested this on both IGEL OS 12 and IGEL OS 11 and it works well on both!

Nothing describes it better than a video! The below will first show the script in action on a
IGEL OS 12 device, then switch to a IGEL OS 11 device. I simulate the time zone change by first setting a time zone in IGEL OS Setup, then I disconnect the network, and connect the network again. I’m simulating this on Virtual IGEL OS machines in my VMWare ESXi. Take a look:

For now, I have not been traveling since figuring this out, but it does have a good chance of succeeding. I can see a potential source of error when connecting to a network that doesn’t give immediate access to the Internet, e.g. captive portal Wi-Fi. If so, the script can be set to run with a hotkey, or an icon in IGEL OS. And, you always have the possibility to correct the time zone manually if all else fails!
Thats it for today! See ya!

/Fred