Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Script Help

Status
Not open for further replies.

bigkid123

ISP
Nov 5, 2002
18
US
Hi,

I am trying to create a sript that will search the /etc/hosts file on my unix server for a host entry and output just the ip of that host to a file. Here is the script that I have created. Any help would be appreciated.

Thanks


#!/usr/local/bin/perl -w

system ( "pg /etc/hosts|grep LEX1|grep -v grep | awk '{ print $1 }' > /admin/script/install/printerip.out" );

sub readline
{
my($file) = @_;
my($line);
open(IN, $file) || die "Unable to open $file";
$line = <IN>;
close IN;
chomp($line);
return $line;
}

$ip = &readline("/admin/script/install/ipaddress.out");
$hostname = &readline("/admin/script/install/hostname.out");
$lex1 = &readline("/admin/script/install/printerip.out");

($a,$b,$c,$d) = split(/\./,$ip,4);
($storenum) = ($hostname =~ /bst00(\d\d\d)/);

$jd2 = $d + 7;


system( "/usr/sbin/hostent -c '$lex1' -i '$jd2'" );
 
I really don't quite understand why you made this so complicated. Also, "pg" is a paging utility and so it's really not wise to use that to feed pipeline processes like 'grep' and 'awk'.
Again, there is no need to use 'awk' since everything that 'awk' can do, perl can do (and often better).

To process the file you could do something like:
Code:
my $host = shift;
while(<>) {
  chomp;
  next if(/^#/);
  print "$1\n" if(/^(\S+).*$host/);
}
Then if you called it, say, "script.pl" you could run it like this: "perl script.pl hostname < /etc/hosts
 
Sorry TrojanWarBlade,

This script actually does a little more that getting the host ip. The problem I am having is getting the script to grep or find a host in the /etc/hosts file and placing just the ip of that host to a file. One that file is created then my script uses it to change the ip of that host to another ip. Any help on getting that to work would be appreciated.

Thanks
 
Show us how you /etc/hosts file looks like, and we might be able to tell you, how you can find a host (we also want to know depending on what, are you searching for it),and how to place it in another file.

And we also want to know depending on what the ip will change.(and if this change will take place in a third file)



``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I have a suspicion that you missed the subtlety of my code.
It filters /etc/hosts for a hostname and outputs ONLY the IP address.
The input file is /etc/hosts and the output can be anything you want it to be just using redirection.
You obviously are capable of reading and writing files in a perl script so you could easily do that if you wanted.

If you offer more details we can offer a better tailored solution.

Code:
perl script.pl localhost < /etc/hosts > /admin/script/install/ipaddress.out
 
Thanks TrojanWarBlade,

That was it, I just didn't understand what you meant, I tried it and it did what I needed it to. Thanks so much for your help.

Thanks Again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top