shreedarasp has asked for the wisdom of the Perl Monks concerning the following question:

i would like to know the date and time in a remote PC connected to the same network. i searched and found only getting local date and time. by passing remote computer name/IP address i should be able to get remote pc date and time. is there a way to do it?
  • Comment on Getting date and time from other computer

Replies are listed 'Best First'.
Re: Getting date and time from other computer
by moritz (Cardinal) on Sep 07, 2009 at 15:44 UTC
    Some computers offer a "daytime" service on port 13, which just prints the current time. Opening a TCP connection and reading the response should do in that case.

    A better way to obtain the time and synchronize clocks is to install a ntpdate server on the remote machine and an ntpdate client on the local machine - most clients offer an option to just obtain the remote time without modifying the local clock.

    In either case the remote machine has to fulfill some constraints, so either it must do so already, or you need to have administrative access.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Getting date and time from other computer
by JavaFan (Canon) on Sep 07, 2009 at 15:21 UTC
    Maybe.

    It will depend on what services are running on that remote computer, and whether you have access to them. There are all kinds of methods possible, but you'll have to have some cooperation from the remote site.

    It might be much more efficient to talk to the admin(s) in charge of the remote box than for perlmonks to guess at a solution.

Re: Getting date and time from other computer
by Marshall (Canon) on Sep 08, 2009 at 01:55 UTC
    All PC clocks will gradually go "out of sync", just like your wristwatch does but at an accelerated rate because the crystals in PC are cheap. I agree with JavaFan, talk to your sys admin about this. It may be almost a "non-issue".

    I am compelled to ask: Why do you even need this? I can think of some good reasons, I want to hear yours.

    Sync'ing clocks in a network can be complex. Let's say my PC's clock is "too slow" vs "actual time". Ok, I can set my clock forward say 3 seconds and things will probably be fine.

    Now what happens if my PC clock is "too fast" vs "actual time"? If I set my clock back 3 seconds, I will probably cause "time sequence errors" in log files, etc.

    The way "I'm too fast" is handled, is by slowing my clock down until "actual time" catches up with me (ie maybe it takes me 61 actual clock seconds to tick off 60 of my clock seconds). That way all my files and log messages have the same time sequential order.

    Anyway if this really is a network, your time will be the same as the others on the network. I remain curious as to your application.

      I don't know what the OP has in mind, but we recently started working with some embedded system devices. Our device of choice has no clock battery at all, so every time you power up the clock must be reset. These devices are intended to be used remotely over the internet, so first thing it does is start openvpn to create a secure network to it. But without a reasonably close clock, openvpn won't even validate. So we use rdate just after the network starts to grab time off one of our servers.

      Mind you we don't run Perl on our device, but I image there are plenty of real world situations where you just don't have a proper time to start with.

        I think this is a fine example of an application that would need to get the approximate time to "get going". I don't know what the OP has in mind either. I do know that you have a very clear idea of what you are doing and why you are doing it. I remain curious as to the OP's application.
Re: Getting date and time from other computer
by matze77 (Friar) on Sep 07, 2009 at 18:54 UTC

    I wonder why you need the time and date of the machine?
    Is it a Linux/Unix box or a windows-machine? Do you have supervisor rights on that machine?
    A good network should have the same time ...

    MH
      A good network should have the same time ...
      And should (preferably) be stored as UTC if machines are located in multiple time zones. :-)
Re: Getting date and time from other computer
by vishi83 (Pilgrim) on Sep 08, 2009 at 06:41 UTC
    Hi,

    You can use Net::SSH::Perl module. You can ssh to the remote machine in your network and issue the date and time commands.

    http://search.cpan.org/dist/Net-SSH-Perl/lib/Net/SSH/Perl.pm

    Thanks,
    Vishy.

    A perl Script without 'strict' is like a House without Roof; Both are not Safe;