Listing 1: Time synchronizing script
#!/usr/bin/perl
use Socket;
use English;
$remote = 'time.nist.gov';
$port = 37;
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
$line = <SOCK>;
# Port 37 Returns Number of seconds since 1900
# 25567 number of days since 1900 to 1970
$time = unpack("N",$line)-25567*60*60*24;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
if($year > 99)
{
$year-=100;
}
$mon+=1;
$correction=time-$time;
$touch_format=sprintf("%02.2s%02.2s%02.2s%02.2s%02.2s.%02.2s",$year,$mon,$mday,$
hour,$min,$sec);
if($OSNAME eq "MSWin32") {
system("date $mon-$mday-$year");
system("time $hour:$min:$sec");
} else {
system("date $touch_format");
}
print "Changed by $correction seconds\n";
close (SOCK) || die "close: $!";
exit; |