In Perl you can replace a string easily, than your normal counterpart SED editor. With SED you would have to use the -e flag in order to edit the content of your data - then pipe it to a new output file and then finally rename the new output file the appropriate file name again. With Perl this becomes simple to do. All you do is use the Perl -i flag this is a Perl internal that will automatically create a backup of the original file you - all you do is give the backup file name extension. Look at my scripts first line.
Since this is hard coded routine, I have made the variables local to my routine followed by . Finally the reason behind my madness is simple, with Perl your code is portable i.e. I can execute this code on any machine with a Perl compile. Well that’s not 100% true, if I compile this code into a standalone binary I can run the exe on any machine. To do this download Active states perl2exe (this is not freeware) or use Cava Packager this is completely free. Once you compile this code you can distribute it (you got the option to compile it into an auto installer) and any user can run it on windows system without even having Perl locally on their machine. So with Perl you code really becomes portable no more coding for a specific Unix distribution or Windows flavor.
Code below!!!!!
============
#!/usr/bin/perl -i.bkup
use strict;
use warnings;
system(’clear’);
#Declare + Initialization;
#=========================
my $replace_OLD=’2008/02/03′;
my $replace_NEW=’2009/01/20′;
my $num_of_args = @ARGV;
# Print the usage out !!!!
#=========================
print “nnComment: Number of arguments is: $num_of_argsn”;
# Verify number of arguments if zero just exit!!!!
#=================================================
if ($num_of_args == 0)
{ print “nnERROR: No command line arguments passed to the script $0 n”;
print “USAGE: n”;
print ‘Example 1 : ./Replace_Dates.pl inputfile’,”n”;
print ‘Example 2 : ./Replace_Dates.pl /dsk1_app/sys1/*.DAT’,”n”;
exit;
}# END_if
# Now loop through those records !!!!
#=====================================
while () #Start reading in Data
{
s/$replace_OLD/$replace_NEW/g;
print;
} # END_while
print “nDONE. Script: $0n”;
The below Perl script one can quickly modify to look at certain system processes via a windows schedule task. The below script incorporates SNMP traps so it will forward all related alerts to you enterprise management server. See below working example - here I’m checking is the MS SQL server agent (sqlagent.exe AND sqlmangr.exe) is up and running at a given interval via a windows schedule task that runs every 15 minutes.!!!
#!/usr/bin/perl
#
# PROGRAM: Process.pl
# DESCRIPTION: Checks win32 Process Info
# HISTORY
# Ver Date Author Description
# —- ———– ——- ————————————————–
# 1.0 11-Jan-2009 Faizel Isaacs Script will Monitor any
# Give Process on the
# Windows Machine
# And Forward SNMP
# Alerts to Support Staff
# If Process is no longer active
# On system!!!
# —- ———– ——- —————————————————
# Calling all required PERL Modules Needed !!!
use Win32::Process::Info;
use Sys::Hostname;
$host = hostname;
my $pi = Win32::Process::Info->new ();
#===========================================
#== Cleaning Up Previous Temp Files !!! ==#
system(”del F:\Perl\Initial.list”);
system(”del F:\Perl\Refine.list”);
system(”cd F:\Perl”);
#===========================================
# Kicking of the Process Stats !!!
foreach $proc ($pi->GetProcInfo ()) {
print “n”;
foreach (sort keys %$proc) {
open(OUTPUT, ‘>>f:PerlInitial.list’) ||die “can’t write to the file called script_output: $1″;
print OUTPUT “$_ => $proc->{$_}n”;
}
}
close(OUTPUT); # Closing my FileHandle
#==================================================
# Finish with My Process Stats Collection !!!
# Now we specify what we look for (Processes)
# And Filter to a output File I check Later
$pattern = “(sqlagent.exe|sqlmangr.exe|perl.exe|fazel.exe)”; #changes are done here on this line
#===================================================
system(”cd F:\Perl”);
open(INPUT, ‘F:PerlInitial.list’) || die “can’t open the file called Initial.list: $1″;
open(OUTPUT, ‘>>F:PerlRefine.list’) ||die “can’t write to the file called Refine.list: $1″;
while(<INPUT>) {
if ($_ =~(m/$pattern/g)) {
print OUTPUT “$_ n”;
}
}
close(INPUT); #Closing my FileHandle
close(OUTPUT); #Closing my FileHandle
#=============================================================
# Finish Capturing the list of Processes I’m interested IN
# Now we need to loop through them all to see what is running
# If a particular Process is not Found I will raise an Alert!!
#=============================================================
# Now I will go search through my
# Refine.list File for specific Prosses!!
#==============================================================
#================================
# Setting All Variables
# The Below Prc Variables
# I will Raise Alerts For
# If they not Active on the System
#================================
system(”cd F:\Perl”);
$Prc1 = “sqlagent.exe”;
$Prc2 = “sqlmangr.exe”;
$Prc3 = “FAIZEL”;
#=================================
# Will Call Windows Find Command
# For the Processes I’m after
# While searching through my
# Temp file f:PerlRefine.list
#=================================
$look1 = system(”find “$Prc1″ F:\Perl\Refine.list”);
$look2 = system(”find “$Prc2″ F:\Perl\Refine.list”);
$look3 = system(”find “$Prc3″ F:\Perl\Refine.list”);
print “This is my return code for $look1 n”;
#================================
# Now I’m Checking the System
# Return Code, if Code eq 256
# The Process is no longer
# Active on the system
#================================
if ($look1 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc1 is not running on $host”");
}
if ($look2 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc2 is not running on $host”");
}
if ($look3 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc3 is not running on $host”");
}

i-Drive-Technologies is a
Cape Town base (SA) company that provides application
support services for the below XAMPP disciplines.
If 100% application availability is a must for you
get in touch with us!!
We have experience in HP NonStop application support
inclusive of Mediation telecoms support, Unix / Linux distributions.
We offer network file management tools
inclusive of SSH support for WIN32/ solutions.
HTML and PHP encryption software.
If you need to outsource any of your mission critical applications
Contact us.
We offer Perl support for Unix / Linux systems as well as win32