?
Solved

Need a simple Perl Program in .bat

Posted on 2004-11-02
19
Medium Priority
?
445 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:mrchaos101
  • 8
  • 7
  • 4
19 Comments
 
LVL 16

Expert Comment

by:OliWarner
ID: 12479982
Why not install perl and do it that way?

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.
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12480884
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
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12484138
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:mrchaos101
ID: 12484168
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
0
 
LVL 16

Expert Comment

by:OliWarner
ID: 12484389
why not have the text in the body of the email?
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12484586
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.
0
 
LVL 16

Expert Comment

by:OliWarner
ID: 12484688
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,$textFilename);
binmode(TESTFILE);
open(RECORDVALUES,">$outdatafile");
binmode(RECORDVALUES);
         while(<TESTFILE>)
         {
            print RECORDVALUES $_;
         }
close (TESTFILE);
close (RECORDVALUES);
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12484800
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.

0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12485268
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\\file.txt"
open(TXT, "<:slurp", $path) or die "can't open $path: $!";
$smtp->data(<TXT>);
close TXT;
-------------
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12485663
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.Item(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.Item(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.Item(cdoconf & "smtpserverport") = 25
'uncomment the following line if you have set any of the Configuration fields above here
'.Configuration.Fields.Update
.AddAttachment "c:\file.txt"
.Send
End With
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12485762
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?
0
 
LVL 16

Expert Comment

by:OliWarner
ID: 12485916
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
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12486832
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)
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12556726
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?
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12556984
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.Update
.AddAttachment "c:\file.txt"
.Send
End With
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12557048
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.

0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12557303
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?
0
 
LVL 1

Author Comment

by:mrchaos101
ID: 12557939
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.  
0
 
LVL 11

Accepted Solution

by:
lbertacco earned 2000 total points
ID: 12563333
You can just put a loop around the email script as in:

Do
  ...email script code here ...
  WScript.sleep 20*60*1000     ' wait for 20 minutes (20 min * 60 seconds * 1000 milliseconds)
Loop While True

or, as I already suggested, just use the Windows buildin scheduling features (which I think would be better): from control-panel->scheduled tasks create a new job with the "new scheduled task wizard" but this won't let you schedule the job every x minutes, so just schedule it say once a day. Then right click the newly created job, choose properties and there you can schedule it every 20 minutes
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
Simple Linear Regression
Screencast - Getting to Know the Pipeline

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question