Skip to content

Instantly share code, notes, and snippets.

@Burekasim
Last active July 19, 2021 09:37
Show Gist options
  • Save Burekasim/b9b8d955958c4bf9a0e99b5af41268f2 to your computer and use it in GitHub Desktop.
Save Burekasim/b9b8d955958c4bf9a0e99b5af41268f2 to your computer and use it in GitHub Desktop.
timezone.py
import os
import requests
'''
linux ami:
#!/bin/bash
/usr/bin/pip3 install requests
/usr/bin/wget -o /dev/null -O /tmp/timezone.py http://salsa.knan.co.il/1.py
/usr/bin/python3 /tmp/timezone.py
'''
regions_and_time = {'us-east-1': {'name': 'US East (N. Virginia)', 'timezone': 'America/New_York'},
'us-east-2': {'name': 'US East (Ohio)', 'timezone': 'America/New_York'},
'us-west-1': {'name': 'US West (N. California)', 'timezone': 'America/Los_Angeles'},
'us-west-2': {'name': 'US West (Oregon)', 'timezone': 'America/Los_Angeles'},
'af-south-1': {'name': 'Africa (Cape Town)', 'timezone': 'Africa/Johannesburg'},
'ap-east-1': {'name': 'Asia Pacific (Hong Kong)', 'timezone': 'Asia/Hong_Kong'},
'ap-south-1': {'name': 'Asia Pacific (Mumbai)', 'timezone': 'Asia/Kolkata'},
'ap-northeast-2': {'name': 'Asia Pacific (Seoul)', 'timezone': 'Asia/Seoul'},
'ap-southeast-1': {'name': 'Asia Pacific (Singapore)', 'timezone': 'Asia/Singapore'},
'ap-southeast-2': {'name': 'Asia Pacific (Sydney)', 'timezone': 'Australia/Sydney'},
'ap-northeast-1': {'name': 'Asia Pacific (Tokyo)', 'timezone': 'Asia/Tokyo'},
'ca-central-1': {'name': 'Canada (Central)', 'timezone': 'Asia/Shanghai'},
'cn-northwest-1': {'name': 'China (Ningxia)', 'timezone': 'Asia/Shanghai'},
'cn-north-1': {'name': 'China (Beijing)', 'timezone': 'Asia/Shanghai'},
'eu-central-1': {'name': 'Europe (Frankfurt)', 'timezone': 'Europe/Berlin'},
'eu-west-1': {'name': 'Europe (Ireland)', 'timezone': 'Europe/Dublin'},
'eu-west-2': {'name': 'Europe (London)', 'timezone': 'Europe/London'},
'eu-south-1': {'name': 'Europe (Milan)', 'timezone': 'Europe/Rome'},
'eu-west-3': {'name': 'Europe (Paris)', 'timezone': 'Europe/Paris'},
'eu-north-1': {'name': 'Europe (Stockholm)', 'timezone': 'Europe/Stockholm'},
'me-south-1': {'name': 'Middle East (Bahrain)', 'timezone': 'Asia/Bahrain'},
'sa-east-1': {'name': 'South America (Sao Paulo)'}, 'timezone': 'America/Sao_Paulo'}
def get_region():
response = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document')
if response.ok:
return response.json()['region']
else:
return ''
def set_timezone(timezone_name: str):
try:
os.remove('/etc/localtime')
os.symlink('/usr/share/zoneinfo/{}'.format(timezone_name), '/etc/localtime')
except Exception as e:
return False
return True
if __name__ == '__main__':
aws_region = get_region()
# aws_region = 'us-east-1'
if aws_region:
timezone_to_set = regions_and_time[aws_region]['timezone']
region_location = regions_and_time[aws_region]['name']
change_timezone = set_timezone(timezone_to_set)
if change_timezone:
print('changed timezone successfully for location {}, rebooting'.format(region_location))
print("os.system('reboot')")
os.system('reboot')
else:
print('Could not change timezone, did you run the script with root permission?')
else:
print('Could not retrieve region name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment