Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can PERL Be Used To Make Phone Calls? 1

Status
Not open for further replies.

kennygadams

Programmer
Jan 2, 2005
94
US
Hi,

I want to develop a perl script that telephone's me when my server goes down. Can this be done with Perl?

Kenny
 
Perl can be used to make coffee if you have a suitable coffee machine attached and know the API. :)

Provided of course you have a modem attached to a server, and the script is running on the system attached to the modem, not the system that went down... if you know modem-speak you could communicate directly with the serial device, but you may prefer to use something like the Device::Modem module available on CPAN.

Annihilannic.
 
Cool, nice one Annihilannic, star for you.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
This will depend on where you live.

In Ireland (where I am), all the mobile phone providers offer free SMS messages that you can send via their website (with a monthly limit). A number of enterprising hackers have written scripts that will automate this process. Depending on how many servers you're talking about, this limit (it's in the hundreds) should be plenty for alerting you to failures.

I don't know if similar services are available where you are, but it's worth keeping in mind.
 
I can see how you'd use modem-related modules to dial a given number, but how are you proposing to get perl to talk to you down the line when you pick up the phone?

I suggest SMS messaging - or some other text-based communication method - is going to be easier to implement.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
One assumes they want to play a recorded message "warning, warning, danger, danger, server is down Mr Robinson"....

Though be careful of copyright infringment ;-)



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I'm doing something similar with e-mail. If I want notification on my phone, I just add my cell phones SMS address (xxxxxxxxxx@carrier.com)

In my case, I'm looking for a php file which executes a quick script file that returns the status of a few applications.

Granted, this can use some TLC (not the most graceful code), but it works.

Code:
#!/usr/bin/perl
$fullReport = 0;
if ($#ARGV >= 0) {
        $fullReport = 1;
}

require Net::SMTP;
Net::SMTP->import;

#use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use HTTP::Cookies;

my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/80');
$ua->cookie_jar(
        HTTP::Cookies->new(
                file => 'mycookies.txt',
                autosave => 1
        )
);

my $req = GET '[URL unfurl="true"]http://172.16.103.124/server_status.php';[/URL]
my $res = $ua->request($req);
if ($res->is_success) {
        $page[0] = $res->content;
}
else {
        $page[0] = "Error: Could not reach Server1!";
}

$req = GET '[URL unfurl="true"]http://172.16.101.124/server_status.php';[/URL]
$res = $ua->request($req);
if ($res->is_success) {
        $page[1] = $res->content;
}
else {
        $page[1] = "Error: Could not reach Server2!";
}
# You need to fill in the variables.
# Read the perldoc for more info on using SMTP.

$req = GET '[URL unfurl="true"]http://172.16.107.124/server_status.php';[/URL]
$res = $ua->request($req);
if ($res->is_success) {
        $page[2] = $res->content;
}
else {
        $page[2] = "Error: Could not reach Server3!";
}

$req = GET '[URL unfurl="true"]http://172.16.108.124/server_status.php';[/URL]
$res = $ua->request($req);
if ($res->is_success) {
        $page[3] = $res->content;
}
else {
        $page[3] = "Error: Could not reach Server4!";
}

$req = GET '[URL unfurl="true"]http://172.16.102.124/server_status.php';[/URL]
$res = $ua->request($req);
if ($res->is_success) {
        $page[4] = $res->content;
}
else {
        $page[4] = "Error: Could not reach server5!";
}

$test = 0;
for ($i = 0; $i < 5;$i++) {
        if ($page[$i] =~ m/\S*Error:/ ||$page[1] =~ m/\S*Critical/) {
                $test = 1;
                $error = $error."\n".$page[$i];
        }
}
if ($fullReport == 0) {
        if ($test == 1) {
                my $smtp = Net::SMTP->new('xxx.xxx.xxx.xxx') or die $!; #replace with smtp ip address
                $smtp->mail( 'xxxx@xxxx.com' ); #from address
                $smtp->to( 'xxxxxx@xxxxx.com' ); #to address
                $smtp->data();
                $smtp->datasend("To: Operations\n"); #address that gets displayed
                $smtp->datasend("From: xxx\@xxxx.com\n"); #address that gets displayed
                $smtp->datasend("Subject: Server Status\n");
                $smtp->datasend("\n"); # done with header
                $smtp->datasend("Please see the below server status\n".$error);
                $smtp->datasend();
                $smtp->quit(); # all done. message sent.
        }
}
else {
        my $smtp = Net::SMTP->new('xxx.xxx.xxx.xxx') or die $!; #repleace with IP address of smtp server
        $smtp->mail('xxxx@xxxx.com');
        $smtp->to('xxxx@xxxxx.com');
        $smtp->data();
        $smtp->datasend("To: Operations\n");
        $smtp->datasend("From: xxx\@xxxxx.com\n");
        $smtp->datasend("Subject: Daily Server Status\n");
        $smtp->datasend("\n");
        if ($test == 1) {
                $smtp->datasend($page[0]."\n".$page[1]."\n".$page[2]."\n".$page[3]."\n".$page[4]);
        }
        else {
                $smtp->datasend("All servers: OK\n");
        }
        $smtp->datasend();
        $smtp->quit();
}
 
Tender Loving Care, we could all use some of that!

Though I'd recommend using MIME::Lite , I've had problems with Net::SMTP especially if your using the built in SMTP server not a valid emailserver that can handle resend query authentication.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I've never used MIME::Lite. I'll have to check that out. But to be honest, I ran across the SMTP solution on the web, and since it worked for me relatively easily, I put this solution in place and never went back to clean it up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top