Not really. It just seemed like a logic entry point to start with. And yes, If I don't find the given entry in the file I the script will then append to the bottom or by some marker within the file.
#!/usr/bin/perl
######################################################################
#####################################################################
####################################################################
###################################################################
# Set Variable file modification
#****************************************************************
$datefile = `date '+%m%d%y'`;
# Copy current /etc/hosts file to the work directory (/etc/work/hostsf
+ile)
#*************************************************************
# Search and file section for hosts file entry
#Main Program
format STDOUT =
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+>
"*********************************************************************
+"
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"*******************************************************************"
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"****************************************************************"
@|||||||||||||||||||||||||||||||||||||||
"ETC Hosts File Inplace Edit Program"
@|||||||||||||||||||||||||||||||||||||||||||||||||||||
"Inplace Edit (Work Directory: /etc/work/hostfiles)"
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"****************************************************************"
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"*******************************************************************"
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+>>
"*********************************************************************
+*"
.
write;
########################### Main Program Section #####################
+###
print "\n";
print "This program is will copy the current etc/hosts file to the \n"
+;
print "working directory (/etc/work/hostfile) to perform the \"Inplace
+\,\" \n";
print "edit of the new or modified directories. \n";
print "\n";
print "Should I continue with the process, or exit for another time? (
+Yes|No): ";
$choice1 = <STDIN>;
chomp($choice1);
while ($choice1 =~ m/No|NO|no|N|n/)
{
print "Now exiting the program. \n";
print "Please wait . . . .\n";
`sleep 5`;
exit;
}
print "Please enter the following the following information . . .\n";
print "New System IP Address (Example - 10.12.13.14): ";
$NewIP = <STDIN>;
chomp($NewIP);
print "Absolute name of system or device (Example - neptune.itdo.pri):
+ ";
$NewLgName = <STDIN>;
chomp($NewLgName);
print "Short name of system or device (Example - neptune): ";
$NewStName = <STDIN>;
chomp($NewStName);
######################## Array #######################################
+##
@input = ($NewIP, $NewLgName, $NewStName);
######################## Sub Routine Call Section ####################
+###
©File;
&editPlace(*input);
########################### Sub Routine Section ######################
+###
sub copyFile
{
`cp /etc/hosts /etc/work/hostfiles/hosts`;
$TargetDir = "/etc/work/hostfiles";
$TargetFile = "/etc/work/hostfiles/hosts";
`cp $TargetFile /etc/work/hosts.workcopy`;
`chmod 777 /etc/work/hosts.workcopy`;
`mv $TargetFile $TargetFile.orig.$datefile`;
}
sub editPlace
{
#$datefile = `date '+%m%d%y'`;
local(*thisIP) = @_;
$WorkCopy = "/etc/work/hosts.workcopy";
open(HOSTS, $WorkCopy) || die "Can't open hosts.workcopy $!\n"
+;
LINE: while(<HOSTS>)
{
next LINE if /^#/;
next LINE if ! /$thisIP[0]/;
($IP, $longhostname, $shorthostname, $devicealias)=split("
+");
print "$IP\t $longhostname\n";
}
close(HOSTS);
}
It's not done, but you can see the idea arising.
|