/etc/hosts file extraction and searching on systems

T

torajeshkg

Hi Perl Gurus,

I'm a starter in perl and i have just finished reading a perl book and
for me getting back the pattern matching is becoming nightmare :)
specially with the /`\/*+ what not .

my problem
========
I have /etc/hosts file and i want to extract only host name from the
/etc/hosts file and then i want to search or make sure that all are in
some levels or i should be able to search something in common among all
these machines.

for example

say i have 100 machines in /etc/hosts file and i want to findout
kerlnel levels which are less than 2.4 or some number and then print
all those host names.
or something like this

the program should ask

1. what is the command you want to run
2. what is the string that you want to grep
3.do you want this to be placed in a file output

something like this userinteractive is much better :)

can this be done with CGI also ?

i know i have asked too much :)
please help me
thx in advance for any help.
grk
 
H

Henry Law

I'm a starter in perl and i have just finished reading a perl book and
for me getting back the pattern matching is becoming nightmare :)
specially with the /`\/*+ what not .

Don't worry; it will become easier. Well, it will if you practice.

Have you read the posting guidelines for this group? (Hint: they are
posted to this group regularly). They tell you that you will get most
help by trying to write your program FIRST; then when you have a problem
getting it to do what you want, you make a small version of it, one
which runs stand-alone and shows the problem, and post it here. Lots of
really helpful people then jump in and make suggestions to help you
learn how to write Perl. (And to debug your program too, as it happens).

So don't wait for answers here; you may not get what you want. Fire up
your editor and write some code!
my problem
========
I have /etc/hosts file and i want to extract only host name from the
/etc/hosts file and then i want to search or make sure that all are in
some levels or i should be able to search something in common among all
these machines.

Think of it as a file which contains a number of lines, each one of
which contains "tokens" separated by white space. You want to examine
one or more of the tokens and print the line if you get a match.
the program should ask

1. what is the command you want to run
2. what is the string that you want to grep
3.do you want this to be placed in a file output

Here are some suggestions to get you started (untested, will not compile)

#! /usr/bin/perl
use strict;
use warnings;

# Read in the things you want to use in the program, using some lines
# like ...
print "Enter thing:";
my $thing = <STDIN>;

# Now open your /etc/hosts file and read it line by line. Something
# like ...
open (HOSTS,"</etc/hosts") or die "Some error message $!";
while (my $hostline = <HOSTS>) {

chomp $hostline; # Common beginner mistake!

# Separate that line into its tokens
my @tokens = split /\t/,$hostline; # Assumes one tab separator

# Check the tokens for what you want to find, using some
# code like
foreach my $token (@tokens) {
if $token =~ /somepattern/ {
# If it matches do some printing

Actually if all you want to do is print out the line from /etc/hosts
then AWK would do just as well (!)
can this be done with CGI also ?

You mean "Can I develop a web page, with associated CGI program, that
will do this?" and the answer is "yes, of course: try it and see". But
you will need a web server running on the machine whose hosts file you
want to read.
i know i have asked too much :)

Not really; but you've not asked it in a way that will get you the most
help.
 
U

usenet

for example

say i have 100 machines in /etc/hosts file and i want to findout
kerlnel levels which are less than 2.4 or some number and then print
all those host names.

Is this a purely hypothetical example? Because I don't really grok
this question - the hosts file does not contain information about the
kernel version (or any other such info) about the machines it resolves.
The only way to get that information is to iterate over you hosts file
and then, for each host, rexec a query on the remote machine (and, of
course, you must authenticate to those hosts). Is THIS what you were
planning to do?
 
G

grk

thx very much Henry for your reply, and sorry for all the gurus asking
without proper code of mine , actually this was my first question and i
was not aware.
i will do my homework and get back!
Thx indeed and wish you all a very happy new year!
grk
 
G

grk

sorry for confusion david,
yes you are correct that a hosts file will not have kernel , but what i
want to do is to extract only hostnames one by one and check if @what
kernel level they are and get a report based on user input at first
stage of program.

i can tell you that i have hundreds of machines and it might be helpful
for me to get a report and findout how many of them are @what level of
kernel levels .

hope this clears my mistake.

grk
perl pujari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top