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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl confusion 1

Status
Not open for further replies.

LAdProg2005

Programmer
Feb 20, 2006
56
US
I am new to perl programming. I am trying to write a script in perl to use certain information from the log file and save it in an output file which is comma delimited. I looked at tutorials and examples but I found nothing concret.

Does anyone know how to grep certain information from a file which is not already delimited somehow? Let me know if more information is needed.

--p
 
More information is needed ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I looked at many tutorials but I am still confused on how to get the exact words from a log file. I have files that keeps track of lot of user information and their hosts. But I onlly need to fetch couple of things from the file but the way it is set I am having hard time finding something strong enough to do the work.

for example:

host 123.45.23.244 {

user info
host info
dns info etc...

}

as well as :
user merry-1 {
dns matching info from above
}

now it is not any kind of delimited file. this is how the representation looks like but i couldn't find a function that would do the work of just getting user info and using dns matching to put the two entries together. Any kind of help is appreciated. IF any more info needed let me know...
 
A sample of the logfile would help ... other than that we're guessing

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
/*This file is automatically generated
*runs nitely
*other stuff
*/

lease 147.129.21.451 {
starts 3 2006/01/18 05:57:46;
ends 3 2006/01/18 06:57:46;
binding state active;
next binding state free;
hardware ethernet 00:03:21:20:cd:65;
uid "\001\000\003%$\315V";
set ddns-fwd-name = "YOUR-49C8C8412E.rh.uic.edu";
set ddns-txt = "31551cb91b44543b8d1ea2972d811083cb";
set ddns-rev-name = "144.21.162.132.in-addr.arpa";
client-hostname "YOUR-40C9C8212E";
}

There may be more than one entry for a given user's MAC address. There should be a separate entry for each device .

host hjohn-1 {
dynamic;
hardware ethernet 00:03:21:20:cd:65;
}


If she registered a second device, its entry would begin "host hjohn-2 {". I want to produce a comma-delimited text file in the format "user, device #, mac addr, ip addr, lease start, lease end". The script would run locally on the machine, and be able to accept input for which file to process and where to put the results - something like "script inputfile outputfile"

To be exact this is what the file has but many more entries than this.
 
Code:
open FH, "<logfile.txt";
open FH2, ">detail.txt";
while (<FH>) {
  if (/lease/) {
     @words=split /\S+/, $_;
     $lease=$words[1];
  }
  ...
  if (/client-hostname/) {
     @words=split /\S+/, $_;
     $client=$words[1];
     print FH2, "$lease, $start, $end ... $client\n";
  }[code]
And build up your variables like that (...)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Ok again, thanks for the help you are a life saver. But as I said I am new inline comments would be nice. What do you mean build up variables like that? Also I have a question. THrough code representation: How does it check line by line and get the information from it. For example starts 3 2006/01/18 05:57:46. And then when I want to save it in comma delimited file how do I join the entries by the name? Plus there are more than one lease for a person, How do i check?

I know this is asking a lot but I have been trying since two weeks and this is the only progress I see besides trying hopelessly how to fetch CORRECT data.

thanks again.
 
I forgot to say:

The log files reside in /var/lib/dhcp/archive, with
names in the format dhcpd.leases-YYMMDD where YY = year, MM = month, and DD = day. Is it ok if it is not .txt?

Thanks a lot :)
 
Code:
open FH, "<logfile.txt";
open FH2, ">detail.txt";
while (<FH>) {   [COLOR=red]#read through file line by line[/color]
  if (/lease/) { [COLOR=red]#if the line contains the word lease, then[/color] 
     @words=split /\S+/, $_;[COLOR=red] #get the lease variable by splitting the[/color]
     $lease=$words[1];      [COLOR=red] #line on whitepace[/color]
  }
 [COLOR=red]  ... #do the same for other variables here[/color]
  if (/client-hostname/) {
     @words=split /\S+/, $_;
     $client=$words[1];
     print FH2, "\"$lease\", \"$start\", \"$end\" ... \"$client\"\n";
      [COLOR=red]#when we track the client line, output the record, and overwrite the record until the client line comes around again[/color]
  }
}
close FH2;
close FH1;

HTH


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
You forgot one more question :) The two entires are connected through hardware, rite? How do we check the hardware with the lease entry hardware and connect the two entries?

Also, as asked before The log files reside in /var/lib/dhcp/archive, with names in the format dhcpd.leases-YYMMDD where YY = year, MM = month, and DD = day. Is it ok if it is not .txt?

You are awesome. Thanks code makes sense :)
 
Paul,

you are using the upper case S in the regexp, which is incorrect:

@words=split /\S+/, $_;

\S = non white space
\s = white space
 
How do you know that the host information ends within the curly brakets? Does it even matter if we are checking by th word?
 
I think I got confused when replying. The file has two kinds of information shown above but they are not in that order. I was only trying to demonstrate that the host is connected to ip lease through hardware. Otherwise the file has all the leases listed first and the host listed after the leases end. That is why I got confused as to if it matters checking that the host is connected to lease or just using the word matching to get the information.

Wouldn't the script get confused as to which user has what lease?
 
Just try it and see, you can learn from the process as well ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
OK, I am trying it since yesterday. Amazes me the language. so many languages with syntax difference only. But if I have question I will be back :) hoping only to get more help :)

THanks infinitly
 
No problem, that's why we're here ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
i get a no comma allowed after file handle at ./dhcp.pl line 32. LIne 32 is : print FH2, "print FH2, "\"$lease\", \"$start\", \"$end\", \"$ethernet\", \"$host\"\n";

As far as I understand the error one of the vars is wrong. But when I check they are all defined. What is going on here? :(
 
Ok I guess I fixed my own problem. Except now when I open the detail file I get:

"This", "", "", "hardware" "hjohn-1" so everything except host name is wrong :( Where am I going wrong? Is there anything else I have to do to make it work?
 
post your latest code ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
But it isn't any different than what you posted except for start end and ethernet if statements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top