Skip to content

Instantly share code, notes, and snippets.

@s3va
Last active April 6, 2023 22:49
Show Gist options
  • Save s3va/1ff8428b701816c9fa24c40c0fdc666b to your computer and use it in GitHub Desktop.
Save s3va/1ff8428b701816c9fa24c40c0fdc666b to your computer and use it in GitHub Desktop.
perl script to get unix time (port 37)
#!/usr/bin/perl
use POSIX qw(strftime);
use IO::Socket::INET;
### binmode(STDIN);
$serverip = $ARGV[0];
if(not defined $serverip){
$serverip = '192.168.46.114';
};
$socket = new IO::Socket::INET (
PeerHost => $serverip,
PeerPort => 'time',
Proto => 'tcp',
) or die "$!\n";
@nettime = localtime( unpack("N", <$socket>));
# @nettime = localtime( unpack("N", $_));
# nettime count seconds from 1900
# localtime count second from 1970
@nettime[5]-=70;
# Why day of month one less?
@nettime[3]+=1;
print strftime("TCP: %F %T",@nettime) . "\n";
$socketudp = new IO::Socket::INET (
PeerHost => $serverip,
PeerPort => 'time',
Proto => 'udp',
Type => SOCK_DGRAM,
) or die "$!\n";
$socketudp->send("");
$udpbuff;
$readedlength = $socketudp->read($udpbuff,4);
print("readed from udp socket " . $readedlength . " bytes\n");
@nettimeudp = localtime( unpack("N", $udpbuff));
@nettimeudp[5]-=70;
@nettimeudp[3]+=1;
print strftime("UDP: %F %T",@nettimeudp) . "\n";
print "time: " . time . "\n";
### print unpack("N",$_) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment