combine grep with the watch and netstat command

ID : 274537

viewed : 31

Tags : grepwatchgrep





Top 4 Answer for combine grep with the watch and netstat command

vote vote

96

For the grep you need:

"grep gateway\|MultiMedia" 

So perhaps try:

watch -n1 'netstat -upnlt | grep "gateway\|MultiMedia"' 
vote vote

80

There's also the new way of doing things... grep -E is nice and portable (Or egrep, which is simply quick for grep -E on linux&bsd) so you don't have to escape the quote. From the man pages:

-E Interpret pattern as an extended regular expression (i.e. force grep to behave as egrep).

So...

watch "netstat -upnlt | grep -E 'gateway|multimedia'"

or

watch "netstat -upnlt | egrep 'gateway|multimedia'"

vote vote

79

I had a similar problem monitoring an ssh connection.

> netstat -tulpan|grep ssh tcp        0      0 192.168.2.52:58072     192.168.2.1:22          ESTABLISHED 31447/ssh  

However watch -n 1 'netstat -tulpan|grep ssh' shows no output (apart from message from watch). If I change it to watch -n 1 'netstat -tulpan|grep ":22"' I get the required output line. It seems as if the -p option is ignored when netstat is run through watch. Strange.

vote vote

63

Top 3 video Explaining combine grep with the watch and netstat command







Related QUESTION?