return to first page linux journal archive
keywordscontents

Listing 5. Getting the Time Perl Script

#!/usr/bin/perl
# Get the time from a time server
# Marcel Gagné, Salmar Consulting Inc., 1999
$remote_host="chime.utoronto.ca";
$remote_port="13";
$hostname=`hostname`;
chomp $hostname;
use IO::Socket;
$socket = IO::Socket::INET->new(
    PeerAddr => $remote_host,
    PeerPort => $remote_port,
    Proto    => "tcp",
    Type     => SOCK_STREAM)
  or die "Could not connect to " .
  "$remote_host:$remote_port: $@\n";
# Get the time
$the_time = <$socket>;
print "The time is $the_time\n";
$local_time = `date`;
print "Your system, $hostname, thinks the" .
 "correct \n date and time is $local_time.\n";
print "Resetting date and time ...\n";
$new_date = `date -s "$the_time"`;
print "New date is $new_date";
# Close the socket connection if open
close($socket);