Perl Ping Script To Ping Remote Server Or Website


Below is a perl script which will allow you to ping a remote server. The article is a follow up article to my php ping script article. This article is similar in that we will also be pinging a remote website to see it if up and running, however this time we will be using perl’s Net module.

Perl Ping Script To Ping Remote Server Or Website

Here is a surprisingly simple perl code snippet to perform a perl ping request to check if a remote server or website is up. It creates a new Net::Ping object and then sends pings the server. We can optionally specify a port to see if a specific port is responding on the server which can be very useful if we want to know if a service is still up and running such as MySQL or Apache.

Perl Ping Specific Port

By using the perl ping script to ping a specific port, we can test wether a service on the server is up and running. For example, port 3306 would test for MySQL, 80 and 443 would test the webserver and 21 would test FTP.

A Perl Ping Script can be very useful and it can be used to make sure a website or server is up and can make our lives easier if we are monitoring multiple servers.

#!/usr/bin/perl -w

use Net::Ping;

# Host can be either an IP or domain name
my $host = "";
#optionally specify a timeout in seconds (Defaults to 5 if not set)
my $timeout = 10;

# Create a new ping object
$p = Net::Ping->new("icmp");

# Optionally specify a port number (Defaults to echo port is not used)

# perform the ping
if( $p->ping($host, $timeout) )
        print "Host ".$host." is aliven";
        print "Warning: ".$host." appears to be down or icmp packets are blocked by their servern";

# close our ping handle


In the above example, we ping port 80. This will test if the website is active. As stated above, changing from port 80 to 3306 for example, would then test for an active MySQL service running.

Real World Unix Timestamp Examples


Unix timestamps are extremely useful and can be used across my different programming languages and platforms. I will outline a couple programming languages that support timestamps.

  • PHPconvert timestamps to date in php
  • MysQLview example on how you can query timestamps from a MySQL Database
  • Javascriptthe Date() function in Javascript can use timestamps
  • Perlthe time() function in Perl can handle unix timestamps
  • Linuxreturn current timestamp in linux

Real World Unix Timestamp Examples


In PHP, you can convert a unix timestamp to a real time by using the php date() function. date() allows you to convert a timestamp that is supplied as the second parameter or if the second parameter is left out, it will convert the current timestamp to date.


/* this would display a time of 23 May 2011 17:12:pm */
echo date('d F Y H:i:a',1306134726);
/* and this would display the current time in the same format as above */
echo date('d F Y H:i:a');


The way the date is displayed is controlled by the flags passed to the function. Here we used the flags ‘d’, ‘F’, ‘Y’, ‘H’, ‘i’ and ‘A’. For a complete list of flags and to see a complete description of this function, visit
The PHP function time(), strtotime() and mktime() all return the current unix timestamp. If you wanted to, you can also get a time stamp 24 hours from now by using the following code.


/* get tomorrows timestamp */
$tomorrow = time() + (24 * 60 * 60);

/* or you can use strtotime to convert a natural string into a timestamp */
$tomorrow = strtotime('+24 hours');

/* and this will create a timestamp based on the date and time variables */
$timestamp = mktime($hour,$min,$sec,$month,$day,$year);



If you have a table that contains a column with a unix timestamp in it, you can convert it using the query below. It will return a column that contains rows of easy to read date and times.

SELECT FROM_UNIXTIME(column_name) AS real_time FROM `table_name`;


Javascript has a function called ‘Date()’ which allows you to use timestamps in javascript. Remember that since javascript is client side code (i.e. runs on the persons browser), the date shown is their own computer time, not the server time. To get the current timestamp in javascript, simple use

<script type="text/javascript">

/* get the current timestamp */
var timestamp = +new Date();


You can also convert a timestamp to a date by using the following javascript date functions.

<script type="text/javascript">

/* create a new date as of the current date */
var date = new Date();
/* you can also create a new date and pass a timestamp to create the date as of the timestamp */
var another_date = new Date(1306482441);

var day  = date.getDate(); // get the day that date relates to
day = day < 10 ? '0' + day : day; // add a 0 if less that 10
var month = date.getMonth() + 1; // returns month as 0 - 11
var year = date.getFullYear(); // 4 digit year (eg. 2011)
var hour = date.getHours(); // get hours
hour = hour<10?'0'+hour:hour; // pad with a 0
var minute = date.getMinutes(); // and minutes
minute = minute<10?'0'+minute:minute; // pad with 0
var second = date.getSeconds(); // get seconds
second = second<10?'0'+second:second; // pad with 0



In order to retrieve the current timestamp in Perl, you can make use of the time() function which is the same as PHP.

my $timestamp = time();


Using the ‘date’ command in linux will print out the date specified by the parameters passed to it. We will use the %s flag to tell it to print the seconds passed since ‘1970-01-01 00:00:00 UTC’ (i.e. it will print out the current timestamp).

date +%s