Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-24-2004, 08:16 AM
|
#1
|
|
LQ Newbie
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21
Rep:
|
strange pipe behaviour
hi,
I want to extract IP address from string
Quote:
|
Oct 24 13:53:13 guliver sshd[29796]: Did not receive identification string from ::ffff:131.211.32.242
|
This string is from /var/log/secure, and I want to use it with iptables to block IPs that are testing ssh login.
Here is my problem. I'm tail-ing /var/log/secure with command
Quote:
tail -f /var/log/secure | awk '/Did not receive identification string from / {print $12}'
::ffff:211.180.157.251
::ffff:193.77.102.137
...
|
but when I add sed to remove pattern ::ffff:, output from pipe is empty
Quote:
|
tail -f /var/log/secure | awk '/Did not receive identification string from / {print $12}' | sed -e s/::ffff://
|
strange thing is that cat instead tail works perfectly
Quote:
cat /var/log/secure | awk '/Did not receive identification string from / {print $12}' | sed -e s/::ffff://
211.180.157.251
193.77.102.137
|
Where is the problem?
|
|
|
|
10-24-2004, 09:55 AM
|
#2
|
|
LQ Addict
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320
Rep:
|
You might need to escape the colons in order for them to be read properly:
sed -e s/\:\:ffff\://
Another option, if your input is always in this format, is to skip sed and just use awk with a specified field separator (a colon):
Code:
tail -f /var/log/secure | awk -F : '/Did not receive identification string from / {print $NF}'
|
|
|
|
10-24-2004, 01:57 PM
|
#3
|
|
LQ Newbie
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21
Original Poster
Rep:
|
thanks for tip,
bat that didn't solve my problem
Quote:
|
tail -f /var/log/secure | awk -F : '/Did not receive identification string from / {print $NF}'
|
returns IP address, but command
Quote:
|
tail -f /var/log/secure | awk -F : '/Did not receive identification string from / {print $NF}' >> /path/file
|
don't write to file...
|
|
|
|
10-24-2004, 03:21 PM
|
#4
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
How about something like this ....
tail /var/log/secure | grep -e "Did not" | awk -F: '{print$4$7}'
Edit: I added the grep part to cut out the stuff you don't need.
Last edited by homey; 10-24-2004 at 04:50 PM.
|
|
|
|
10-26-2004, 03:39 AM
|
#5
|
|
LQ Newbie
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21
Original Poster
Rep:
|
try to redirect output of
tail -f /var/log/secure | awk -F : '/Did not receive identification string from / {print $NF}'
to some file
tail -f /var/log/secure | awk -F : '/Did not receive identification string from / {print $NF}' >> /tmp/test
nothing happens, /tmp/test is empty, why?
|
|
|
|
10-26-2004, 09:37 AM
|
#6
|
|
LQ Addict
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320
Rep:
|
What is the purpose of "/Did not receive identification string from /"?
I tried it without this, and it seems to work. With that part included, I get no redirect
|
|
|
|
10-26-2004, 10:59 AM
|
#7
|
|
LQ Newbie
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21
Original Poster
Rep:
|
please read the beginning of the thread
|
|
|
|
10-26-2004, 12:54 PM
|
#8
|
|
LQ Addict
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320
Rep:
|
Sorry, but that didn't answer my question. I don't know all uses for awk, so I don't understand why you added the "/Did not.../ " string to the awk command.
It would help a lot in fixing your problem, considering that when the string is removed, the awk and redirect work fine. I'm thinking it's a syntax problem.
So...what are you hoping to achieve by using
awk -F : '/Did not receive identification string from / {print $NF}'
instead of
awk -F : '{print $NF}'
?
I guess the question would be
What exactly do you want the output to look like?
|
|
|
|
10-27-2004, 12:03 PM
|
#9
|
|
LQ Newbie
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21
Original Poster
Rep:
|
Quote:
Originally posted by mikshaw
Sorry, but that didn't answer my question. I don't know all uses for awk, so I don't understand why you added the "/Did not.../ " string to the awk command.
It would help a lot in fixing your problem, considering that when the string is removed, the awk and redirect work fine. I'm thinking it's a syntax problem.
So...what are you hoping to achieve by using
awk -F : '/Did not receive identification string from / {print $NF}'
instead of
awk -F : '{print $NF}'
?
|
with awk -F : '/Did not receive identification string from / {print $NF}'
I'm filtering lines with string "Did not receive identification string from"
Ok, I can do that also with grep before awk, but anyway
tail -f /tmp/test | awk -F : '{print $NF}' >> somefile
don't write to somefile
Quote:
I guess the question would be
What exactly do you want the output to look like?
|
I need to extract IP address from line:
Oct 24 13:53:13 guliver sshd[29796]: Did not receive identification string from ::ffff:131.211.32.242
and then store it to somefile
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:54 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
|
|