Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Perl code to fetch time from NTP server

By: a guest on Jan 18th, 2014  |  syntax: Perl  |  size: 2.73 KB  |  views: 91  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2. # ntpdate.pl
  3.  
  4. # this code will query a ntp server for the local time and display
  5. # it.  it is intended to show how to use a NTP server as a time
  6. # source for a simple network connected device.
  7.  
  8. #
  9. # For better clock management see the offical NTP info at:
  10. # http://www.eecis.udel.edu/~ntp/
  11. #
  12.  
  13. # written by Tim Hogard (thogard@abnormal.com)
  14. # Thu Sep 26 13:35:41 EAST 2002
  15. # this code is in the public domain.
  16. # it can be found here http://www.abnormal.com/~thogard/ntp/
  17.  
  18. $HOSTNAME=shift;
  19. $HOSTNAME="192.168.1.254" unless $HOSTNAME ;    # our NTP server
  20. $PORTNO=123;                    # NTP is port 123
  21. $MAXLEN=1024;                   # check our buffers
  22.  
  23. use Socket;
  24.  
  25. #we use the system call to open a UDP socket
  26. socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
  27.  
  28. #convert hostname to ipaddress if needed
  29. $ipaddr   = inet_aton($HOSTNAME);
  30. $portaddr = sockaddr_in($PORTNO, $ipaddr);
  31.  
  32. # build a message.  Our message is all zeros except for a one in the protocol version field
  33. # $msg in binary is 00 001 000 00000000 ....  or in C msg[]={010,0,0,0,0,0,0,0,0,...}
  34. #it should be a total of 48 bytes long
  35. $MSG="\010"."\0"x47;
  36.  
  37. #send the data
  38. send(SOCKET, $MSG, 0, $portaddr) == length($MSG)
  39. or die "cannot send to $HOSTNAME($PORTNO): $!";
  40. ##print "sent message\n";
  41.  
  42. $portaddr = recv(SOCKET, $MSG, $MAXLEN, 0)      or die "recv: $!";
  43. ##print "got msg\n";
  44. ##($portno, $ipaddr) = sockaddr_in($portaddr);
  45. ##$host = gethostbyaddr($ipaddr, AF_INET);
  46. ##print "$host($portno) said $MSG\n";
  47. ##print "$host($portno) said something \n";
  48.  
  49. #We get 12 long words back in Network order
  50. @l=unpack("N12",$MSG);
  51.  
  52.  
  53. ##foreach(@l) {
  54. ##printf("%08x ",$_);
  55. ##print "\n" if($x++%4==3);
  56. ##}
  57.  
  58. #The high word of transmit time is the 10th word we get back
  59. #tmit is the time in seconds not accounting for network delays which should be
  60. #way less than a second if this is a local NTP server
  61. $tmit=$l[10];   # get transmit time
  62. ##print "tmit=$tmit\n";
  63.  
  64. #convert time to unix standard time
  65. #NTP is number of seconds since 0000 UT on 1 January 1900
  66. #unix time is seconds since 0000 UT on 1 January 1970
  67. #There has been a trend to add a 2 leap seconds every 3 years.  Leap
  68. #seconds are only an issue the last second of the month in June and
  69. #December if you don't try to set the clock then it can be ignored but
  70. #this is importaint to people who coordinate times with GPS clock
  71. #sources.
  72.  
  73. $tmit-= 2208988800;    
  74.  
  75. #printf("%d\n", $tmit);
  76. #use unix library function to show me the local time (it takes care of
  77. #timezone issues for both north and south of the equator and places that
  78. #do Summer time/ Daylight savings time.
  79.  
  80. print scalar localtime ($tmit);
  81. print "\n";
  82.  
  83. #compare to system time
  84. ##$t=time();
  85. ##print " $t  $tmit ",$t-$tmit,"\n";
clone this paste RAW Paste Data