Simple Perl Ping Script – Local and Remote

A Perl Beginner Template to play with
This is just a little script I slung together to play with LWP::SIMPLE and NET::PING Perl modules. The main idea is to get info from a website (dyndns.org), clean it up and use it for either input or output. I also wanted a few bits of code for user (choice) input as well. It’s another base that a beginner can build on.

Ask and Go Fetch
It asks for input and based on the reply performs one of two different tasks and offers some info. Nothing more, nothing less. However, if you look at the actions it performs, you will see that this basic script can be adapted to acquire any type of information from any website, which in turn can be used for a completely different script.

#!/usr/bin/perl
#
# This script will get a local or remote IP, ping the address and tell you if the ADSL line is up
# By Rich Scadding  @ http://debianandi.blogspot.com
#
#######################
# PERL MODULES TO USE #
#######################
# Ping Module
use Net::Ping;
# Get Html
use LWP::Simple;# Debian apt-get install libwww-perl
# Debugging
#use strict; (commented as it throws up errors)
use warnings;
#######################

# Check if an ip adress is alive
print “Which IP shall I check? Yours [1] Other [2]: “;
my $answer = ;
chomp $answer;
# If the idiot can’t type, tell them!
die “No, only press 1 or 2. Try again!\n” if ($answer ne “1” and $answer ne “2”);
# 2 needs an IP from the user
if ($answer eq “2”) {
print “Type the IP Address: “;
my $IP = ;
chomp $IP;
print “Checking their IP\n”;
my $p = Net::Ping->new(“icmp”);
print “$IP is alive.\n” if $p->ping($IP);
print “$IP is as dead as a Dodo.\n” unless $p->ping($IP);
$p->close();
}
# 1 will get the external IP from dyndns.org
if ($answer eq “1”) {
my $html;
$html = get( ‘http://checkip.dyndns.org’ );
chomp $html;
print “Checking your IP\n”;
# Print the result from the html file
$html =~ s/.*ddress: //;
$html =~ s/<\/body.*//;
print “$html\n”;

# Remove the html tags from the message and set the resulting IP as $address
my $address = $html;
chomp $address;
$address =~ s/.*ddress: //;
$address =~ s/<\/body.*//;

# Print a ping message
print (“Pinging $address\n”);

# Ping the IP address and report if it is up
my $p = Net::Ping->new(“icmp”);
print “$address is alive.\n” if $p->ping($address);
print “$address is as dead as a Dodo.\n” unless $p->ping($address);
$p->close();
}

As I said, quite basic, and you could just manually ping an IP from the terminal, no need for a script. However, as with most scripts and one-liners, it’s about finding out how to perform certain actions which can be applied to other uses.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s