Skip to content

Instantly share code, notes, and snippets.

@ruairif
Created February 13, 2014 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruairif/8974941 to your computer and use it in GitHub Desktop.
Save ruairif/8974941 to your computer and use it in GitHub Desktop.
Is NTP blocked on your network? Have you no access to a GPS? Does your computer complain about SSL certs? Then you might just need a quick and dirty time setting script.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''\
Instructions:
Make this script Executable::
chmod +x set_date.py
Place this script in /opt::
cp set_date.py /opt/
To set this script to run every 5 minutes as a cron job as root::
crontab -e
Then add the following line::
*/5 * * * * /opt/set_date.py
'''
import sys
from subprocess import call
PY2 = sys.version_info[0] == 2
if PY2:
from urllib2 import urlopen
else:
from urllib.request import urlopen
def set_date(time):
command = ['date', '--set', time]
call(command)
def get_date(url='http://google.com'):
resp = urlopen(url)
return resp.headers['date']
if __name__ == '__main__':
set_date(get_date())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment