mrchaos101
asked on
Need a simple Perl Program in .bat
I am in need of a very simpel perl program. Im limmited in my programming know how. I took 1 semister of Perl 5 in college and it seemd to be a very nice easy language... I need a basic app Im sure you experts could type out in like 10 lines of code and in 2 min ehehhe....
Below are some Scalars I have used in class that I would like you to use if you need them...
I need it to be somefile.bat When I run it from a comand propt it will display PROGAM RUNING or something on the command window.
When the .bat is run it should start have a count down timer in seconds.... I will edit it to set the var to the actual time I want
please set it to count down from say 10min.... I will modifity the timer when I need in the code.
At the end of timmer it should EMAIL a .txt file. I will edit it and provide the name of the txt file and location of the text file.
please set the file to send to c:\temp.txt
I will eddit the code for the email address I need and file I need sent.
I want it to send me an .txt file every 20-30 min. THe text file alwasy ahs same name.
my $email = "self\@home.com"; # Your email account
my $support = "send\@isp.com,$email"; # Where Txt File is to go to addresses
my $msvr = "mail"; # mail.isp.what ever.. this is the SMTP/POP3 Server Var
Let me know if you need more info thanks.
Below are some Scalars I have used in class that I would like you to use if you need them...
I need it to be somefile.bat When I run it from a comand propt it will display PROGAM RUNING or something on the command window.
When the .bat is run it should start have a count down timer in seconds.... I will edit it to set the var to the actual time I want
please set it to count down from say 10min.... I will modifity the timer when I need in the code.
At the end of timmer it should EMAIL a .txt file. I will edit it and provide the name of the txt file and location of the text file.
please set the file to send to c:\temp.txt
I will eddit the code for the email address I need and file I need sent.
I want it to send me an .txt file every 20-30 min. THe text file alwasy ahs same name.
my $email = "self\@home.com"; # Your email account
my $support = "send\@isp.com,$email"; # Where Txt File is to go to addresses
my $msvr = "mail"; # mail.isp.what ever.. this is the SMTP/POP3 Server Var
Let me know if you need more info thanks.
As far as I know there no way to send an email from a .bat without additional external tools.
So you have the following options:
- use a VBscript script in place of a batch file. So you don't need any additional utility
- or install some additional tool, such as
+ an external mail program (and then stick to batches)
+ a perl interpreter, so you can just use your script
+ an enhanced command shell, such as JPsoft (www.jpsoft.com) 4nt, which includes a sendmail program
So you have the following options:
- use a VBscript script in place of a batch file. So you don't need any additional utility
- or install some additional tool, such as
+ an external mail program (and then stick to batches)
+ a perl interpreter, so you can just use your script
+ an enhanced command shell, such as JPsoft (www.jpsoft.com) 4nt, which includes a sendmail program
ASKER
I have perl set up on my pc now.
I have an applacation that sends me an email every when my isp is off line for x amount of item. I just dont know how to MODIFY it ot send a TEXT file vs. Making an email.
here is the app.... I am hoping sombody can help me modify it.
Please note the orgianl content of the code came from
Version 1.1, 08/23/2000, by Dave Methvin
# See http://www.winmag.com/fixes/2000/0818.htm
I have made personal changs to it...
__________________________ __________ __________ __________ __________ __________ _
@rem = '
@echo off
if exist %0.bat goto NOEXT
perl %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
:NOEXT
perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
@rem ';
my $gate = "10.10.10.10"; # Ip of ISP gateway or any IP on the net you want to ping
my $complain = 0; # Email if outage >= $complain minutes
my $email = "self\@home.com"; # Var for my email Address
my $support = "support\@isp.com,$email"; # Var for where were emial will be sent
my $name = "Your Name"; # Your name for email
my $phone = "(800)555-1212"; # Your phone number
my $msvr = "mail"; # Mail server name or IP
# All of these packages are installed by default with ActivePerl
# for Win32, available at http://www.activestate.com/. You may
# need to load your own packages for other versions.
use strict;
use Win32::OLE;
use Net::Ping;
use Net::SMTP;
# Create ping object
my $p = Net::Ping->new("icmp")
or die("new ping failed");
my $last;
PING: {
Win32::Sleep(1*60*1000);
my $alive = $p->ping($gate) || $p->ping($gate);
if ( $alive && $last ) {
my $out = int((time - $last + 30)/60);
status("outage ended, $out minutes");
complain($last, $out) if $complain && $out >= $complain;
$last = 0;
}
elsif ( !$alive ) {
if ( !$last ) {
$last = time;
status("outage started");
}
redo PING; # ping faster if we're down
}
Win32::Sleep(1*60*1000);
print scalar(localtime), " [OK]\n";
redo PING;
}
# need some way to signal exit
$p->close();
exit(0);
sub status {
my ($hr, $dy, $mn, $yr) = (localtime)[2..5];
open(F, sprintf(">> cm%04d%02d.txt",
$yr+1900, $mn+1)) or warn("log file, $!");;
print F scalar(localtime), ": ", join(" ", @_), "\n";
print scalar(localtime), ": ", join(" ", @_), "\n";
if ( $speak && $hr >= $speak_hr_start && $hr <= $speak_hr_end ) {
$voice->speak("$alert $_[0]", 1);
}
close(F);
}
sub complain {
my ($last, $mins) = @_;
my $died = scalar localtime $last;
my $smtp = Net::SMTP->new($msvr, Hello => $msvr) or
do { warn("Net::SMTP failed for server '$msvr'\n"), return };
$smtp->mail($email);
$smtp->to(split(/,/, $support));
$smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption
This email is being sent from
$name, $email.
A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.
Logs have been made.
If you have any more questions contact $name through $email
or call $phone.
BODY
$smtp->quit or warn("SMTP quit failed");
}
__END__
:END_BATCH_FILE
I have an applacation that sends me an email every when my isp is off line for x amount of item. I just dont know how to MODIFY it ot send a TEXT file vs. Making an email.
here is the app.... I am hoping sombody can help me modify it.
Please note the orgianl content of the code came from
Version 1.1, 08/23/2000, by Dave Methvin
# See http://www.winmag.com/fixes/2000/0818.htm
I have made personal changs to it...
__________________________
@rem = '
@echo off
if exist %0.bat goto NOEXT
perl %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
:NOEXT
perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
@rem ';
my $gate = "10.10.10.10"; # Ip of ISP gateway or any IP on the net you want to ping
my $complain = 0; # Email if outage >= $complain minutes
my $email = "self\@home.com"; # Var for my email Address
my $support = "support\@isp.com,$email";
my $name = "Your Name"; # Your name for email
my $phone = "(800)555-1212"; # Your phone number
my $msvr = "mail"; # Mail server name or IP
# All of these packages are installed by default with ActivePerl
# for Win32, available at http://www.activestate.com/. You may
# need to load your own packages for other versions.
use strict;
use Win32::OLE;
use Net::Ping;
use Net::SMTP;
# Create ping object
my $p = Net::Ping->new("icmp")
or die("new ping failed");
my $last;
PING: {
Win32::Sleep(1*60*1000);
my $alive = $p->ping($gate) || $p->ping($gate);
if ( $alive && $last ) {
my $out = int((time - $last + 30)/60);
status("outage ended, $out minutes");
complain($last, $out) if $complain && $out >= $complain;
$last = 0;
}
elsif ( !$alive ) {
if ( !$last ) {
$last = time;
status("outage started");
}
redo PING; # ping faster if we're down
}
Win32::Sleep(1*60*1000);
print scalar(localtime), " [OK]\n";
redo PING;
}
# need some way to signal exit
$p->close();
exit(0);
sub status {
my ($hr, $dy, $mn, $yr) = (localtime)[2..5];
open(F, sprintf(">> cm%04d%02d.txt",
$yr+1900, $mn+1)) or warn("log file, $!");;
print F scalar(localtime), ": ", join(" ", @_), "\n";
print scalar(localtime), ": ", join(" ", @_), "\n";
if ( $speak && $hr >= $speak_hr_start && $hr <= $speak_hr_end ) {
$voice->speak("$alert $_[0]", 1);
}
close(F);
}
sub complain {
my ($last, $mins) = @_;
my $died = scalar localtime $last;
my $smtp = Net::SMTP->new($msvr, Hello => $msvr) or
do { warn("Net::SMTP failed for server '$msvr'\n"), return };
$smtp->mail($email);
$smtp->to(split(/,/, $support));
$smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption
This email is being sent from
$name, $email.
A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.
Logs have been made.
If you have any more questions contact $name through $email
or call $phone.
BODY
$smtp->quit or warn("SMTP quit failed");
}
__END__
:END_BATCH_FILE
ASKER
On a side note...
this is all done in not pade of course.. when I save the file I give it a name.bat it runs great.
It is just I want another program to run on a different computer to send me a .txt file ever so often so I can review it.
Tim
this is all done in not pade of course.. when I save the file I give it a name.bat it runs great.
It is just I want another program to run on a different computer to send me a .txt file ever so often so I can review it.
Tim
why not have the text in the body of the email?
ASKER
Ok here is the situation.
I am a DIE HARD Ever Quest fan. I leave my player in what is called TRADER MODE while I am at work. Basicly he has items for sale at price I set. When something is sold there is a .txt file that gets a log. The every time something is sold it is added to the END of the txt file. I want to be able to check it from work.
Ever so often I want it to email me a copy of that .txt file.
I am a DIE HARD Ever Quest fan. I leave my player in what is called TRADER MODE while I am at work. Basicly he has items for sale at price I set. When something is sold there is a .txt file that gets a log. The every time something is sold it is added to the END of the txt file. I want to be able to check it from work.
Ever so often I want it to email me a copy of that .txt file.
well there's a little bit of code that will read in a text file...
Just modify the "print RECORDVALUES $_;" bit to throw that into the body of an email...
open(TESTFILE,$textFilenam e);
binmode(TESTFILE);
open(RECORDVALUES,">$outda tafile");
binmode(RECORDVALUES);
while(<TESTFILE>)
{
print RECORDVALUES $_;
}
close (TESTFILE);
close (RECORDVALUES);
Just modify the "print RECORDVALUES $_;" bit to throw that into the body of an email...
open(TESTFILE,$textFilenam
binmode(TESTFILE);
open(RECORDVALUES,">$outda
binmode(RECORDVALUES);
while(<TESTFILE>)
{
print RECORDVALUES $_;
}
close (TESTFILE);
close (RECORDVALUES);
ASKER
WOOOT that may work..
OK
Im not good with paths.... I asume it would be
Can you show me where to put the path say..
OPEN c:\temp\test.txt I want to read all data in that file into the email.
OK
Im not good with paths.... I asume it would be
Can you show me where to put the path say..
OPEN c:\temp\test.txt I want to read all data in that file into the email.
There is a simpler way: change lines
---------
$smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption
This email is being sent from
$name, $email.
A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.
Logs have been made.
If you have any more questions contact $name through $email
or call $phone.
BODY
--------------
to:
-------------
$path = "c:\\path_to_your_file\\fi le.txt"
open(TXT, "<:slurp", $path) or die "can't open $path: $!";
$smtp->data(<TXT>);
close TXT;
-------------
---------
$smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption
This email is being sent from
$name, $email.
A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.
Logs have been made.
If you have any more questions contact $name through $email
or call $phone.
BODY
--------------
to:
-------------
$path = "c:\\path_to_your_file\\fi
open(TXT, "<:slurp", $path) or die "can't open $path: $!";
$smtp->data(<TXT>);
close TXT;
-------------
However using VBScript might be easier. This script (sendfile.vbs) does it (well, should...). Remember to change email addresses, smtp server and filepath strings). By default, the smtp server configured in Outlook express is used, but you can change that uncommenting the configuration lines:
----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message" )
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
'Uncomment the following line if you need to set any of the configuration fields below
'cdoconf="http://schemas.microsoft.com/cdo/configuration/"
'Uncomment the following line to force external smtp server. Default is use local SMTP service if available
'.Configuration.Fields.Ite m(cdoconf & "sendusing") = 2
'uncomment and update the following line if you want to set a different smtp server. Default is the one configured in OExpress
'.Configuration.Fields.Ite m(cdoconf & "smtpserver") = "mail.myprovider.com"
'uncomment and update the following line if you want to use a different smtp server port. Default is 25
'.Configuration.Fields.Ite m(cdoconf & "smtpserverport") = 25
'uncomment the following line if you have set any of the Configuration fields above here
'.Configuration.Fields.Upd ate
.AddAttachment "c:\file.txt"
.Send
End With
----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message"
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
'Uncomment the following line if you need to set any of the configuration fields below
'cdoconf="http://schemas.microsoft.com/cdo/configuration/"
'Uncomment the following line to force external smtp server. Default is use local SMTP service if available
'.Configuration.Fields.Ite
'uncomment and update the following line if you want to set a different smtp server. Default is the one configured in OExpress
'.Configuration.Fields.Ite
'uncomment and update the following line if you want to use a different smtp server port. Default is 25
'.Configuration.Fields.Ite
'uncomment the following line if you have set any of the Configuration fields above here
'.Configuration.Fields.Upd
.AddAttachment "c:\file.txt"
.Send
End With
ASKER
Tacco
The vb script is a possablity... can it be run from a command prompt?
Also I need it in a loop
I need to repeat the code every 20 min or so?
The vb script is a possablity... can it be run from a command prompt?
Also I need it in a loop
I need to repeat the code every 20 min or so?
The best way to get this running every 20 minutes is make a bat file to call compile/run the script. then call that bat file frm windows's scheduled tasks
It can be run from the command prompt as in
C:\> myscript.vbs
I can easily wrap a loop around it, but in my opinion it's much better to schedule it to run every 20minutes using
control-panel->scheduled tasks (first create a new job with the "new scheduled task wizard" but this won't let you schedule the job every x minutes, so jsut schedule it say once a day. Then right click the newly created job, choose properties and there you can schedule it every 20 minutes)
C:\> myscript.vbs
I can easily wrap a loop around it, but in my opinion it's much better to schedule it to run every 20minutes using
control-panel->scheduled tasks (first create a new job with the "new scheduled task wizard" but this won't let you schedule the job every x minutes, so jsut schedule it say once a day. Then right click the newly created job, choose properties and there you can schedule it every 20 minutes)
ASKER
lbertacco
I like what you posted...
but the thing is the only way it triggers an email send is AFTER there has been an outtage.
any other advice on what to add or change on that?
I like what you posted...
but the thing is the only way it triggers an email send is AFTER there has been an outtage.
any other advice on what to add or change on that?
Maybe your default are not appropriate. Try uncommenting the configuration lines in the script I posted (as I've done in the script below) and set your smtp server hostname (plus eventually the port number too) appropriately. I hope this will solve.
Also, can you clarify "AFTER there has been an outtage" ?
----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message" )
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
cdoconf="http://schemas.microsoft.com/cdo/configuration/"
.Configuration.Fields.Item (cdoconf & "sendusing") = 2
.Configuration.Fields.Item (cdoconf & "smtpserver") = "mail.myprovider.com"
.Configuration.Fields.Item (cdoconf & "smtpserverport") = 25
.Configuration.Fields.Upda te
.AddAttachment "c:\file.txt"
.Send
End With
Also, can you clarify "AFTER there has been an outtage" ?
----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message"
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
cdoconf="http://schemas.microsoft.com/cdo/configuration/"
.Configuration.Fields.Item
.Configuration.Fields.Item
.Configuration.Fields.Item
.Configuration.Fields.Upda
.AddAttachment "c:\file.txt"
.Send
End With
ASKER
That script up there is a Netwokr monitor.
It is set to PING a server or IP that I enter every x amount of seconds. If a ping is LOST it logs the loss and keeps pinging.
Once it gets a reply agian it Logs loss over and then emails the information about the outtage to me.
Win32::Sleep(1*60*1000);
print scalar(localtime), " [OK]\n";
redo PING;
that pings it ever 60 seconds I belive.
It is set to PING a server or IP that I enter every x amount of seconds. If a ping is LOST it logs the loss and keeps pinging.
Once it gets a reply agian it Logs loss over and then emails the information about the outtage to me.
Win32::Sleep(1*60*1000);
print scalar(localtime), " [OK]\n";
redo PING;
that pings it ever 60 seconds I belive.
Oh I understand your question. Disregard my last post which is quite meaningless in this case.
However I'm a bit confused now. Are you trying to monitor the status of a remote server with ping, or to email yourself an Ever Quest log files every x minutes?
However I'm a bit confused now. Are you trying to monitor the status of a remote server with ping, or to email yourself an Ever Quest log files every x minutes?
ASKER
HEHE
Ok what it is that i want is to eamail my self and Ever Quest.txt file every X min.
Sombody up there said that he didn't think y ou could email ne Perl... Perl is know for being good with Web/Email etc.
I have a script done in perl that moniters my connection to my ISP. It emails me when it goes down.
I posted that script hopeing he would see the code for how to send an email.
I was then hoping to get get following:
Function that looped
Timer starts
Timer Ends
Sends emial with the file attached.
Loop starts over.
Hope that helps.
Ok what it is that i want is to eamail my self and Ever Quest.txt file every X min.
Sombody up there said that he didn't think y ou could email ne Perl... Perl is know for being good with Web/Email etc.
I have a script done in perl that moniters my connection to my ISP. It emails me when it goes down.
I posted that script hopeing he would see the code for how to send an email.
I was then hoping to get get following:
Function that looped
Timer starts
Timer Ends
Sends emial with the file attached.
Loop starts over.
Hope that helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You wont be able to do this with a bat unless you have a lot of other things to handle email which would also have to be installed.