LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 08-21-2009, 05:50 PM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
netstat - grep - tail -f output log


I'm trying to write a script that will tail to a log. I want to monitor my localmachine listening ports.

I was working with this here:

Code:
netstat -anp | grep 0.0.0.0:* | grep LISTEN
This prints to the screen ... I want to output it to a log to monitor incoming local ports.

I've tried this:

Code:
netstat -anpo 7 | grep 0.0.0.0:* | grep LISTEN
Hoping that this would refresh every 7 seconds on the screen and grep the data.

Oh and I have tried this:

Code:
netstat -anpo 7 >/tmp/test12 | grep "LISTEN" | grep "0.0.0.0:*" | tail -f /tmp/test12
Any idea's how I can leave the terminal open to refresh my output? Are my greps all wrong? or maybe I need to use an "awk"... still learning thx

Last edited by manwithaplan; 08-21-2009 at 06:15 PM.
 
Old 08-21-2009, 06:21 PM   #2
Nevahre
LQ Newbie
 
Registered: Aug 2009
Posts: 21

Rep: Reputation: 16
take a look at netstats c option: netstat -anpc | grep 0.0.0.0:* | grep LISTEN
can be found in the manpage
 
Old 08-21-2009, 06:28 PM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by Nevahre View Post
take a look at netstats c option: netstat -anpc | grep 0.0.0.0:* | grep LISTEN
can be found in the manpage
Thx, I must of missed that ... I didn't want it continuous, wanted to refresh every 5-10 seconds.... and the -o option doesn't work.

Do think I should loop this..?
 
Old 08-21-2009, 06:29 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you want to monitor the changes in the output of netstat from a terminal, take a look at the watch command:
Code:
watch -n7 'netstat -anp | grep -w LISTEN'
if you really need to send the output to a file, you can try an endless loop with a sleep command to run netstat every n seconds and send the output to a file using the tee command. In this way you can see the output on the terminal and store it in a file at the same time:
Code:
#!/bin/bash
while true
do
  netstat -anp | grep -w LISTEN | tee -a test.log
  sleep 7
done
 
Old 08-21-2009, 06:36 PM   #5
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by colucix View Post
If you want to monitor the changes in the output of netstat from a terminal, take a look at the watch command:
Code:
watch -n7 'netstat -anp | grep -w LISTEN'
This actually will work for me thanks ... I wasn't familiar with watch or tee... this should help immensely. I wanted to output to a log, because I wanted to add it to a python script to use libnotify. I want to listen to certain ports ... then use libnotify to popup a message when the port is open and when it has been closed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Howto tail -f /var/log/messages | grep isdninfo Mopp Programming 4 07-22-2011 11:00 AM
Perl or PHP Script that can tail /var/log/auth.log - two-factor authentication tdnnash25 Linux - Server 1 06-18-2009 09:36 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
[SOLVED] tail and grep issue Wim Sturkenboom Linux - General 6 02-06-2008 08:19 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM


All times are GMT -5. The time now is 03:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration