def main(address, v): c = ntplib.NTPClient() response = c.request(address, version=v) print("Response Offset: ", response.offset) print("Version: ", response.version) print("Response (Time): ", ctime(response.tx_time)) print("Leap: ", ntplib.leap_to_text(response.leap)) print("Root Delay: ", response.root_delay) print(ntplib.ref_id_to_text(response.ref_id))
def ntp_logging(): ''' Compute a timestamp using NIST Internet Time Servers as an additional level of assurance for database. This function is called by search function or can be called via XML or JSON remote procedure calls. Example for XMLRPC: >>> from xmlrpclib import ServerProxy >>> s = ServerProxy('http://ml-lab.bioinformatics.org/init/plugin_notalogger/call/xmlrpc') >>> s.ntp_logging() ''' ntp_pool = ['asia.pool.ntp.org', 'europe.pool.ntp.org', 'oceania.pool.ntp.org', 'north-america.pool.ntp.org', 'south-america.pool.ntp.org', 'africa.pool.ntp.org'] client = ntplib.NTPClient() server = random.choice(ntp_pool) response = client.request(server, version=3) try: results = ['Network time server: ' + server, 'Offset : %f' % response.offset, 'Stratum : %s (%d)' % (ntplib.stratum_to_text(response.stratum), response.stratum), 'Precision : %d' % response.precision, 'Root delay : %f ' % response.root_delay, 'Root dispersion : %f' % response.root_dispersion, 'Delay : %f' % response.delay, 'Leap indicator : %s (%d)' % (ntplib.leap_to_text(response.leap), response.leap), 'Poll : %d' % response.poll, 'Mode : %s (%d)' % (ntplib.mode_to_text(response.mode), response.mode), 'Reference clock identifier : ' + \ ntplib.ref_id_to_text(response.ref_id, response.stratum), 'Original timestamp : ' + time.ctime(response.orig_time), 'Receive timestamp : ' + time.ctime(response.recv_time), 'Transmit timestamp : ' + time.ctime(response.tx_time), 'Destination timestamp : ' + time.ctime(response.dest_time)] results = ' | '.join(results) except: results = 'Failure to connect to network time server or there is \ an internet error. Please try again later.' notalogger_db.logger.insert(notarizecode='', name='', pidentifier='', email='', usage='NTP pool server time check', comments=results, datetimeserver=str(server), seconds_since_epoch=str(time.time())) notalogger_db.commit()
def ntp_stamp(): if session.username == None: redirect(URL(r=request, f='../account/log_in')) form = FORM( TABLE( TR('Network Time Servers: ', SELECT('asia.pool.ntp.org', 'europe.pool.ntp.org', 'oceania.pool.ntp.org', 'north-america.pool.ntp.org', 'south-america.pool.ntp.org', 'africa-america.pool.ntp.org', _name='server')), TR(INPUT(_type='submit', _value='Stamp')))) if form.accepts(request.vars, session): from time import ctime import ntplib client = ntplib.NTPClient() try: response = client.request(form.vars.server, version=3) results = ['Network time server: ' + form.vars.server, 'Offset : %f' % response.offset, 'Stratum : %s (%d)' % (ntplib.stratum_to_text(response.stratum), response.stratum), 'Precision : %d' % response.precision, 'Root delay : %f ' % response.root_delay, 'Root dispersion : %f' % response.root_dispersion, 'Delay : %f' % response.delay, 'Leap indicator : %s (%d)' % (ntplib.leap_to_text(response.leap), response.leap), 'Poll : %d' % response.poll, 'Mode : %s (%d)' % (ntplib.mode_to_text(response.mode), response.mode), 'Reference clock identifier : ' + \ ntplib.ref_id_to_text(response.ref_id, response.stratum), 'Original timestamp : ' + ctime(response.orig_time), 'Receive timestamp : ' + ctime(response.recv_time), 'Transmit timestamp : ' + ctime(response.tx_time), 'Destination timestamp : ' + ctime(response.dest_time)] db.log.insert(event='NTP timestamp. ' + ' | '.join(results), user=session.username) db.user_event.insert(event='NTP timestamp. ' + ' | '.join(results), user=session.username) db.entry_hash.insert(eid='NTP', edatetime='NTP', etitle='NTP', ehash='NTP timestamp. ' + ' | '.join(results)) db.comment_hash.insert(cid='NTP', cdatetime='NTP', eid='NTP', chash='NTP timestamp. ' + ' | '.join(results)) except: results = 'Failure to connect to network time server or there is \ an internet error. Please try again later.' session.result = results redirect(URL(r=request, f='ntp_stamp_output')) return dict(form=form)
def stats(): response = get_data() return { "time": asctime(gmtime(response.tx_time)), "offset": response.offset, "leap": ntplib.leap_to_text(response.leap), "delay": response.root_delay, "ref": ntplib.ref_id_to_text(response.ref_id), "stratum": ntplib.stratum_to_text(response.stratum), "timestamp": response.tx_time }
def test_helpers(self): """Helper methods tests.""" client = ntplib.NTPClient() time.sleep(self.POLL_DELAY) info = client.request(self.NTP_SERVER) self.assertEqual(int(info.tx_time), ntplib.ntp_to_system_time( ntplib.system_to_ntp_time(int(info.tx_time)))) self.assertTrue(isinstance(ntplib.leap_to_text(info.leap), str)) self.assertTrue(isinstance(ntplib.mode_to_text(info.mode), str)) self.assertTrue(isinstance(ntplib.stratum_to_text(info.stratum), str)) self.assertTrue(isinstance(ntplib.ref_id_to_text(info.ref_id, info.stratum), str))
def test_helpers(self): """Helper methods tests.""" client = ntplib.NTPClient() time.sleep(self.POLL_DELAY) info = client.request(self.NTP_SERVER) self.assertEqual( int(info.tx_time), ntplib.ntp_to_system_time( ntplib.system_to_ntp_time(int(info.tx_time)))) self.assertTrue(isinstance(ntplib.leap_to_text(info.leap), str)) self.assertTrue(isinstance(ntplib.mode_to_text(info.mode), str)) self.assertTrue(isinstance(ntplib.stratum_to_text(info.stratum), str)) self.assertTrue( isinstance(ntplib.ref_id_to_text(info.ref_id, info.stratum), str))
def print_response(response): print ("Version number : %d" % response.version) print ("Offset : %f" % response.offset) print ("Stratum : %s (%d)" % (ntplib.stratum_to_text(response.stratum), response.stratum)) print ("Precision : %d" % response.precision) print ("Root delay : %f " % response.root_delay) print ("Root dispersion : %f" % response.root_dispersion) print ("Delay : %f" % response.delay) print ("Leap indicator : %s (%d)" % (ntplib.leap_to_text(response.leap), response.leap)) print ("Poll : %d" % response.poll) print ("Mode : %s (%d)" % (ntplib.mode_to_text(response.mode), response.mode)) print ("Python time: %f, %s" % (time.time(), str(datetime.datetime.utcnow()))) print ("Transmit timestamp : " + time.ctime(response.tx_time)) print ("Reference timestamp : " + time.ctime(response.ref_time)) print ("Original timestamp : " + time.ctime(response.orig_time)) print ("Receive timestamp : " + time.ctime(response.recv_time)) print ("Destination timestamp : " + time.ctime(response.dest_time)) print ("Reference clock identifier : " + ntplib.ref_id_to_text(response.ref_id, response.stratum))
def print_response(response): print('Version number : %d' % response.version) print('Offset : %f' % response.offset) print('Stratum : %s (%d)' % (ntplib.stratum_to_text(response.stratum), response.stratum)) print('Precision : %d' % response.precision) print('Root delay : %f ' % response.root_delay) print('Root dispersion : %f' % response.root_dispersion) print('Delay : %f' % response.delay) print('Leap indicator : %s (%d)' % (ntplib.leap_to_text(response.leap), response.leap)) print('Poll : %d' % response.poll) print('Mode : %s (%d)' % (ntplib.mode_to_text(response.mode), response.mode)) print('Python time: %f, %s' % (time.time(), str(datetime.datetime.utcnow()))) print('Transmit timestamp : ' + time.ctime(response.tx_time)) print('Reference timestamp : ' + time.ctime(response.ref_time)) print('Original timestamp : ' + time.ctime(response.orig_time)) print('Receive timestamp : ' + time.ctime(response.recv_time)) print('Destination timestamp : ' + time.ctime(response.dest_time)) print('Reference clock identifier : ' + ntplib.ref_id_to_text(response.ref_id, response.stratum))
def doRun(self): c = ntplib.NTPClient() response = c.request('time.google.com', version=3) self.print_view(response.offset) self.print_view(response.version) self.print_view("서버시간:", ctime(response.tx_time)) self.print_view("시스템시간:", ctime(time.time())) neolib4Win._win_set_time( datetime.datetime.fromtimestamp(int(response.tx_time + response.root_delay)).timetuple()) self.print_view("시스템시간:", ctime(time.time())) self.print_view(response.tx_time - time.time()) self.print_view(ntplib.leap_to_text(response.leap)) self.print_view(response.root_delay) self.print_view(ntplib.ref_id_to_text(response.ref_id))
if len(sys.argv) > 1: hostname = sys.argv[1] else: sys.stdout.write('NTP server ? ') hostname = sys.stdin.readline().strip() client = ntplib.NTPClient() response = client.request(hostname, version=3) print('Version number : %d' % response.version) print('Offset : %f' % response.offset) print('Stratum : %s (%d)' % (ntplib.stratum_to_text(response.stratum), response.stratum)) print('Precision : %d' % response.precision) print('Root delay : %f ' % response.root_delay) print('Root dispersion : %f' % response.root_dispersion) print('Delay : %f' % response.delay) print('Leap indicator : %s (%d)' % (ntplib.leap_to_text(response.leap), response.leap)) print('Poll : %d' % response.poll) print('Mode : %s (%d)' % (ntplib.mode_to_text(response.mode), response.mode)) print('Transmit timestamp : ' + str(response.tx_time)) print('Reference timestamp : ' + str(response.ref_time)) print('Original timestamp : ' + str(response.orig_time)) print('Receive timestamp : ' + str(response.recv_time)) print('Destination timestamp : ' + str(response.dest_time)) print('Reference clock identifier : ' + ntplib.ref_id_to_text(response.ref_id, response.stratum))
#!/usr/bin/env python3 import ntplib import os import mod_log from time import ctime c = ntplib.NTPClient() response = c.request('it.pool.ntp.org') print (response.offset) print (response.version) print (ctime(response.tx_time)) print (ntplib.leap_to_text(response.leap)) print (response.root_delay) print (ntplib.ref_id_to_text(response.ref_id)) print(ctime(response.tx_time)) print(ntplib.system_to_ntp_time) #print (os.system) #os.system('/etc/init.d/ntpd stop, /usr/sbin/ntpdate -b -s it.pool.ntp.org, /etc/init.d/ntpd start') current_time = ctime(response.tx_time) #os.system("sudo date -s '{0}'".format(current_time)) os.system("service ntp stop") os.system(ntpdate .format(current_time) os.system("service ntp start") #self.logger.info('Raptor rozpoczal prace: {0}'.format(current_time)) #os.system('/etc/init.d/ntpd stop, /usr/sbin/ntpdate -b -s it.pool.ntp.org, /etc/init.d/ntpd start')
if __name__ == '__main__': rospy.init_node('ntp_monitor', anonymous=True) rate = rospy.Rate(1) # 10hz monitor = Monitor("ntp_monitor") ntp_servers = rospy.get_param('ntp_servers', rosnode.get_machines_by_nodes()) offset_warn = rospy.get_param('abs_offset_warn', 1.0) offset_error = rospy.get_param('abs_offset_error', 5.0) ntp_client = ntplib.NTPClient() while not rospy.is_shutdown(): for server in ntp_servers: try: response = ntp_client.request(server) error = 0.0 if abs(response.offset) > offset_warn: error = 0.6 if abs(response.offset) > offset_error: error = 1.0 monitor.addValue(server+"/ntp_offset", response.offset, "s", error, 1) monitor.addValue(server+"/ntp_version", response.version, "", 0.0, 1) monitor.addValue(server+"/ntp_time", ctime(response.tx_time), "", 0.0, 1) monitor.addValue(server+"/ntp_time_unix", response.tx_time, "", 0.0, 1) monitor.addValue(server+"/ntp_leap", ntplib.leap_to_text(response.leap), "", 0.0, 1) monitor.addValue(server+"/ntp_root_delay", response.root_delay, "s", 0.0, 1) except (ntplib.NTPException, ntplib.socket.gaierror): monitor.addValue(server+"/ntp_error", "Server not reachable", "", 1.0, 1) rate.sleep()
# # La lista de algunos servidores se hora: # https://tf.nist.gov/tf-cgi/servers.cgi # http://support.ntp.org/bin/view/Servers/StratumOneTimeServers # import datetime from time import ctime import ntplib import os try: servidor_de_tiempo = "https://tf.nist.gov/tf-cgi/servers.cgi" client = ntplib.NTPClient() response = client.request(servidor_de_tiempo, version=4) print("====================================") print("Pidiendo hora a: " + servidor_de_tiempo) print(" Offset : " + str(response.offset)) print(" Version : " + str(response.version)) print(" Date Time : " + str(ctime(response.tx_time))) print(" Leap : " + str(ntplib.leap_to_text(response.leap))) print(" Root Delay : " + str(response.root_delay)) print(" Ref Id : " + str(ntplib.ref_id_to_text(response.ref_id))) os.system("sudo date -s '" + str(ctime(response.tx_time)) + "'") print("Hora actualizada") print("====================================") except: os.system("sudo date") print("NTP Server Down Date Time NOT Set At The Startup") pass