[sf-perl] remote host time

Joe Brenner doom at kzsu.stanford.edu
Sat Jul 5 11:46:37 PDT 2008

Richard Reina <gatorreina at gmail.com> wrote:

> Does anyone know how one might go about getting the date and time of
> a remote host (on my LAN) from within a perl script assuming that I
> know the remote machine's ip address?

Presuming we're talking about a unix-like machine here, it should have
a "date" command with a "--set" feature, which you can run remotely
over ssh, something like this:

  ssh -f user at nn.nn.nn.nn 'date --set=STRING'

(1) if you haven't shared the ssh keys on, this will bug you for a

(2) as I remember it, there's something tricky about getting the format
    of "STRING" right.  The man page for date might not be that
    helpful: expect to experiment.

(3) if "user" doesn't have the permissions to set the date, you will
    have other problems

Doing it "from within perl" is just a matter of running it with
"system" (or with backticks, or qx):

  my $cmd = " ssh ... etc ... ";

Recovering gracefully from errors here is left as an exercise.

But pay attention to Andy and Bill: they're trying to help you solve
your problems, I'm just trying to answer your question.

More information about the SanFrancisco-pm mailing list