PERL: ping and e-mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: ping and e-mail
# 8  
Old 03-24-2003
OK - here we go. The details you require are in the array @pingDetails. It's possibly a bit crude, but it does work!
Code:
#!/perl/bin/perl -w

#use strict;
use Net::SMTP;

# get list of ip's to ping
open (PINGFILE, "< c:\\pinghosts.txt") or die "Can't open PingHost list: $!";

my @pinglist;
my @results;
my @pingDetails;
my $holder;

while (my $line = <PINGFILE>) {
	chomp $line;
	# place the host ipaddress in the array @pinglist
	push @pinglist, $line;
}

# now ping all machines
foreach my $host (@pinglist) {
	# place results of ping in $holder
	$holder = `ping $host`;
	# place contents of $holder into the array @results
	push @results, $holder;
}

foreach my $result (@results) {
	# pull out the IP address and time details
	$result =~/(\d*\.\d*\.\d*\.\d*).*(time\<\d*ms)/;
	#place the results into $details and push into the array
	my $details = $1 . " " . $2 . "\n";
	push @pingDetails, $details;
}

Let me know how it goes Smilie
# 9  
Old 03-24-2003
IT WORKS PERFECT !!! Smilie

now last part. E-mail. i have this from a book but i get a error about the $smtp->mail($from); part.

$from = "colinctdprep@oceanfree.net";
@body = $result;
$subject = "PERL pinghost program report";
{

$smtp = Net::SMTP->new("icmp");
die "Could not make connection: $!" if (defined $smtp);

$smtp->mail($from);
$smtp->to($to);

$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
foreach(@body) {
$smtp->datasend("$_\n");
}
$smtp->dataend();
$smtp->quit;
}


this is for windows NT. can you PLEASE help on this.
It has to open a list of e-mail address and put the @pingDetails into a text file and e-mail it to the e-mail address in the file.
and it has to use colin@oceanfree.net as the sending e-mail.


And i really apprectate the help. Smilie

Last edited by perleo; 03-24-2003 at 02:39 PM..
# 10  
Old 03-24-2003
Try placing a \ symbol before the @ sign in your email address:

$from = "colinctdprep\@oceanfree.net";

The @ symbol may be screwing things up..
# 11  
Old 03-24-2003
I think this will work:

Code:
#get list of e-mail addresses to send e-mails to

opendir (EMAILS, "< c:\\Documents and Settings\\emaillist.txt") || die ("Can't open e-mail list: $!");
my @TO = readdir EMAILS;
closedir (EMAILS);

my $from = "colinctdprep\@oceanfree.net"; 
my $subject = "PERL pinghost program report";

foreach my $to (@TO) {

  $smtp = Net::SMTP->new("icmp");
  die "Could not make connection: $!" unless $smtp;

  $smtp->mail($from);
  $smtp->to($to);

  $smtp->data();
  $smtp->datasend("To: $to\n");
  $smtp->datasend("From: $from\n");
  $smtp->datasend("Subject: $subject\n");
  $smtp->datasend("\n");

  foreach(@pingDetails) {
    $smtp->datasend("$_\n");
  }

  $smtp->dataend();
  $smtp->quit();

}

Reference: http://perl.about.com/library/weekly/aa022000a.htm
# 12  
Old 03-25-2003
still wont work oombera.

WIntellect have u got an awnser to this problem ?
# 13  
Old 03-25-2003
Smilie Hmm...

What error message are you getting now? Still the $smtp->mail($from); one?

If not, then you may want to try changing the email addresses inside the emaillist.txt file so that each address has a backslash before the @ symbol as well.. or create a small file and experiment with just a few addresses.
# 14  
Old 03-25-2003
every time it open the emaillist.txt is erases the e-mail addresses inside it.

I canged that part to the chomp to take the output and put it ina array and still the same, it erases the addresses in the file. I tried the \ before the @ in the addresses and still erases the addresses. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. Shell Programming and Scripting

Perl : code on ping showing difference result

Hi all, I am using the below code to ping a code and print whehter the connection is successful or not. use Net::Ping; $p = Net::Ping->new(); my $host = "x.x.x.x"; # print "$host is alive.\n" if $p->ping($host); if ($p->ping($host,3)) { print... (0 Replies)
Discussion started by: scriptscript
0 Replies

3. Shell Programming and Scripting

Perl : ping for multiple IPs not working

I have written perl ping program to ping list of IPs one by one and print the status.But each and every time it is showing the status as Pass for all IPs even though the IP is wrong. multipleip.pl use Net::Ping; $p = Net::Ping->new(); $ifile="inventory.txt"; ... (2 Replies)
Discussion started by: scriptscript
2 Replies

4. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

5. Shell Programming and Scripting

Ping and Perl

Hi There i have little situation that i could us some help with. We have a dhcp server, but the problem is if that a machine has been offline for a while it loose it's lease and so if you ping it you get unknown host if there a way using perl that it will continue to try and ping it, even tho... (1 Reply)
Discussion started by: ab52
1 Replies

6. Programming

Perl Ping Loop

Hi All i have an issue with ping, we are using dhcp and so if the machine has been offline and i ping it, i get " ping: unknown host <hostname> is there a way i can stick a loop somewhere so it would keep trying when it got the unknown host error and then when the machine came back online... (2 Replies)
Discussion started by: ab52
2 Replies

7. Shell Programming and Scripting

perl return ips after successful ping

Hi, I have this script in ksh, what it does is loop every ip in the nodes_nso and produced another variable up_nodes_nso of only ip's that are up. nodes_nso=$(cat /var/tmp/nodes.txt) echo "ICMP Tests:" up_nodes_nso="" for ip in ${nodes_nso} ; do ping ${ip} 3 > /dev/null if ; then ... (1 Reply)
Discussion started by: borderblaster
1 Replies

8. Shell Programming and Scripting

perl ping script

Dear All Any one able been writing any command ping in perl??basically i want to wring ping script to send 1000 packet ping then initiate "Ctrl C' terminal to ping example below:-] #!/usr/local/bin/perl $r=`/bin/ping 172.23.11.254`; Thank You ---------- Post updated at 10:27 PM ----------... (2 Replies)
Discussion started by: netxus
2 Replies

9. Shell Programming and Scripting

Perl telnet to cisco router and compare the ping ms

All Please help, i will telnet to router to obain the ping status and compare, if higher than normal latency, i will have further action.. if i do the telent and in perl script then .... e.g the result i obtain from the router will be =' Success rate is 100 percent (5/5), round-trip... (4 Replies)
Discussion started by: optimus
4 Replies

10. Shell Programming and Scripting

Perl Ping Determine Success or Fail

I know how to ping in Perl. That is easy. What I am wondering is if there is a way for Perl to determine whether the ping was successful or not. Or do I need to save the results out and parse the results seperately looking for the #of tries and successful revieves. Thanks. (1 Reply)
Discussion started by: gdboling
1 Replies
Login or Register to Ask a Question