LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   netstat - grep - tail -f output log (https://www.linuxquestions.org/questions/linux-networking-3/netstat-grep-tail-f-output-log-749396/)

manwithaplan 08-21-2009 04:50 PM

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

Nevahre 08-21-2009 05:21 PM

take a look at netstats c option: netstat -anpc | grep 0.0.0.0:* | grep LISTEN
can be found in the manpage

manwithaplan 08-21-2009 05:28 PM

Quote:

Originally Posted by Nevahre (Post 3652980)
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..?

colucix 08-21-2009 05:29 PM

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


manwithaplan 08-21-2009 05:36 PM

Quote:

Originally Posted by colucix (Post 3652989)
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.


All times are GMT -5. The time now is 08:38 PM.