Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-18-2007, 07:05 AM
|
#1
|
LQ Newbie
Registered: Aug 2007
Posts: 8
Rep:
|
convert LAN IP address to Host Name when I give cmd tail -f /var/log/squid/access.log
I am using Fedora 4. I have configured squid as a proxy server for my official LAN. When I monitor the log I want the local IP's hostname when it sends http request to proxy server.How can I convert LAN IP address to Host Name when I give command
tail -f /var/log/squid/access.log?
|
|
|
08-19-2007, 02:40 AM
|
#2
|
Member
Registered: Aug 2005
Location: Ukraine
Distribution: Ubuntu 9.04
Posts: 99
Rep:
|
Code:
#!/usr/bin/perl
use strict;
use IO::File;
use Fcntl qw(:seek);
use Socket;
my $fh = IO::File->new('/var/log/squid/access.log');
my $line;
while(1) {
$fh->seek(0,1);
$line = $fh->getline();
if ($line) {
chomp $line;
my (@fields) = split(' ', $line);
my $addr = $fields[2];
my $name = gethostbyaddr(inet_aton($addr), AF_INET);
if ($name) {
$fields[2] = $name;
}
print join(' ', @fields), "\n";
}
}
simple tail -f in perl with resolver requests
Have fun 
|
|
|
08-19-2007, 02:46 AM
|
#3
|
Member
Registered: Aug 2005
Location: Ukraine
Distribution: Ubuntu 9.04
Posts: 99
Rep:
|
Code:
use strict;
use IO::File;
use Fcntl qw(:seek);
use Socket;
my $fh = IO::File->new('/var/log/squid/access.log');
my $line;
while(1) {
$fh->seek(0,1);
$line = $fh->getline();
if ($line) {
chomp $line;
my (@fields) = split(' ', $line);
my $addr = $fields[2];
my $name = gethostbyaddr(inet_aton($addr), AF_INET);
if ($name) {
$fields[2] = $name;
}
print join(' ', @fields), "\n";
}
else {
sleep 1;
}
}
I've added sleep call because the previous version eats 100% CPU 
|
|
|
08-19-2007, 05:41 AM
|
#4
|
LQ Newbie
Registered: Aug 2007
Posts: 8
Original Poster
Rep:
|
Quote:
Originally Posted by rtg
Code:
use strict;
use IO::File;
use Fcntl qw(:seek);
use Socket;
my $fh = IO::File->new('/var/log/squid/access.log');
my $line;
while(1) {
$fh->seek(0,1);
$line = $fh->getline();
if ($line) {
chomp $line;
my (@fields) = split(' ', $line);
my $addr = $fields[2];
my $name = gethostbyaddr(inet_aton($addr), AF_INET);
if ($name) {
$fields[2] = $name;
}
print join(' ', @fields), "\n";
}
else {
sleep 1;
}
}
I've added sleep call because the previous version eats 100% CPU 
|
Thank you very much for your quick reply. But there are too many things in /usr/lib/perl. How and where should I insert this code? For your kind information I am very new linux environment.
Regards
rs15
|
|
|
08-19-2007, 09:20 AM
|
#5
|
Member
Registered: Aug 2005
Location: Ukraine
Distribution: Ubuntu 9.04
Posts: 99
Rep:
|
Ah, no, you should not put the script to the perl lib path.
Just save it as a file in your home directory (say squid-tail.pl) , then add
to the beginning of the script.
Change the rights to be 0755 -
Code:
chmod 0755 squid-tail.pl
and just run it under the user that has access to /var/log/squid:
./squid-tail.pl
or
perl squid-tail.pl -the script will start printing out all the lines from the log trying to resolve the ip addresses.
Feel free to ask if something is not quite clear.
|
|
|
08-20-2007, 02:46 AM
|
#6
|
LQ Newbie
Registered: Aug 2007
Posts: 8
Original Poster
Rep:
|
yeh...., I have done the job, at last. Thanks a lot. It shows the hostname instead of IP. But when I put the command perl squid-tail.pl, it executes the whole access.log file. Is it possible to show only desired part or latest part of access.log.
Regards
RS15
|
|
|
01-22-2012, 01:45 AM
|
#7
|
Member
Registered: Sep 2005
Location: Bangladesh
Distribution: RH 7.2, 8, 9, Fedora
Posts: 227
Rep:
|
Now a little help please
guys,
Thanks for code. That is fantastic. But here are the two things...
How much resource it will actually take?? I have a Local DNS and resolving is damn fast infact. using this, using Vyatta Squid and SARG, i can track my hosts (300 of them) easily. But again, as it is a continuously running script with a forever while loop, how can i put it integrated so that it will do the lookup on a controlled manner, like a cron job, as first it will be executed then the "sarg-reports daily" in hour basis. This way, I dont want to keep it running for ever and to run it just when i'm making the log analysis.
I call this an another way to save resource.
I'm running centos 5.7 on a esxi 4.1
Mishu~
|
|
|
All times are GMT -5. The time now is 08:46 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|