Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine grep with the watch and netstat command

Tags:

Red Hat Enterprise Linux Server release 5.4 (Tikanga) 2.6.18-164.el5

I am trying to use the watch command combined with the netstat to see the 2 programs using certain ports.

However, with the command I using below doesn't work for both words:

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

Is this the correct way to grep for both program names.

If I use one its ok, but both together doesn't work.

like image 341
ant2009 Avatar asked Sep 27 '12 04:09

ant2009


People also ask

How do I combine two grep commands?

Use "extended grep" and the OR operator ( | ). Yes, you're right.

What does netstat grep do?

Using the Netstat Command Typically, Netstat displays all the ports in use by all processes, however, by adding the grep command, you can limit those results to only a specific designated port.

What is use of netstat command in Linux?

The network statistics ( netstat ) command is a networking tool used for troubleshooting and configuration, that can also serve as a monitoring tool for connections over the network. Both incoming and outgoing connections, routing tables, port listening, and usage statistics are common uses for this command.

What does watch command do?

The watch command is a built-in Linux utility used for running user-defined commands at regular intervals. It temporarily clears all the terminal content and displays the output of the attached command, along with the current system date and time.


2 Answers

For the grep you need:

"grep gateway\|MultiMedia"

So perhaps try:

watch -n1 'netstat -upnlt | grep "gateway\|MultiMedia"'
like image 70
Bitwise Avatar answered Sep 19 '22 21:09

Bitwise


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'"

like image 39
Dmitri DB Avatar answered Sep 16 '22 21:09

Dmitri DB