LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2006, 03:45 AM   #1
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
tail with -f switch | awk


Hi guys! I have a problem with the piece of code below:

I am confused as to what could be the problem because it simply won't work when I use this in a script but when I type this directly on the command line then this script works as expected. I need your help to make this work.. Thanks!


Code:
tail -f /var/log/messages |  awk '/Invalid user/ {print $10}'
 
Old 12-18-2006, 03:58 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Could you post the script you are using? I ask because the following seems to work:

Code:
 #!/bin/bash

tail -f /var/log/allmessages | awk '/stasis login/ { print $6 }'
This code snippet prints the name of the user that logs in.
 
Old 12-18-2006, 04:15 AM   #3
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Original Poster
Rep: Reputation: 20
Sorry I did miss something... this is the actual code I'm using..

abc=$(tail -f /var/log/messages | awk '/Invalid user/ {print $10}')

I tried the -n switch as a replacement and it works but that is not what I need. I need to follow the logs and make some action to it.

Last edited by yongitz; 12-18-2006 at 04:16 AM.
 
Old 12-18-2006, 04:20 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Tail -f is a continuously running process, you cannot put that into a variable.
You could redirect the output to a file, regularly check this file for changes and act accordingly. This can be done from one script.

Hope this helps.
 
Old 12-18-2006, 04:29 AM   #5
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Original Poster
Rep: Reputation: 20
Thanks druuna! I guess my purpose is going to be defeated if that's the case...
I need to use it for auto blocking the IP of those who are trying to get in my ssh server. Though I can do it through cron maybe run it every minute but I want a real time scan of those attempts and block them automatically. Well, thanks again for your time..
 
Old 12-18-2006, 04:23 PM   #6
zefo
Member
 
Registered: Mar 2003
Posts: 71

Rep: Reputation: 15
shorewall firewall may do this automatically for you. check it out. /joe
 
Old 12-18-2006, 06:42 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could pipe the output into your cmd for doing the blocking.
 
Old 12-18-2006, 07:06 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
There are tons of better methods for your purpose, most of which have already been discussed here at LQ (my favorite is the iptables-recent module).
 
Old 12-19-2006, 03:50 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
what is the significance of print $10? In the cdorrectly working example of drunna, it is printing $6, the sixth variable of every line returned by tail.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tail +40 /etc/FILENAME newbie_mel Linux - Software 1 03-20-2006 06:25 AM
root-tail help adds2one Linux - Software 5 12-28-2005 12:36 AM
thread switch results in kernel stack switch superstition Linux - General 1 05-17-2005 11:48 PM
set up DSL thru SWITCH - winXp connected to SWITCH too husz Linux - Newbie 5 04-22-2004 12:08 AM
tail -f dcoder Programming 8 09-01-2003 08:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration