Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nc command to monitor server port in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use nc command to monitor server port in Linux". In daily operation, I believe many people have doubts about how to use nc command to monitor server port in Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use nc command to monitor server port in Linux". Next, please follow the editor to study!

I. the usage of the nc command to detect the port

# nc-v-w 10% ip%-z% port%

-v shows the instruction execution process.

-w sets the time to wait for connection.

-u means to use the udp protocol

-z uses 0 input / output mode and is used only when scanning communication ports.

Example 1: scan the designated port 8080

The copy code is as follows:

# nc-v-w 10-z 192.168.0.100 8080

Connection to 192.168.0.100 8080 port [tcp/http] succeeded!

Example 2: scan the port range from 20 to 25 and output in detail.

The copy code is as follows:

# nc-v-w 2-z 192.168.0.100 20-25

Nc: connect to 192.168.0.100 port 20 (tcp) failed: connection refused

Nc: connect to 192.168.0.100 port 21 (tcp) failed: connection refused

Connection to 192.168.0.100 22 port [tcp/ssh] succeeded!

Nc: connect to 192.168.0.100 port 23 (tcp) failed: connection refused

Nc: connect to 192.168.0.100 port 24 (tcp) failed: connection refused

Nc: connect to 192.168.0.100 port 25 (tcp) failed: connection refused

Example 3: scan the port range from 1 to 65535 and output only the open ports (remove the-v parameter)

The copy code is as follows:

# nc-w 1-z 192.168.0.100 1-65535

Connection to 192.168.0.100 22 port [tcp/ssh] succeeded!

Connection to 192.168.0.100 80 port [tcp/http] succeeded!

Connection to 192.168.0.100 2121 port [tcp/scientia-ssdb] succeeded!

Connection to 192.168.0.100 4004 port [tcp/pxc-roid] succeeded!

Connection to 192.168.0.100 8081 port [tcp/tproxy] succeeded!

Connection to 192.168.0.100 11211 port [tcp/*] succeeded!

Second, batch test the opening of the designated port of the server:

1. If we want to monitor a bunch of specified ip and ports, we can create a new file (column 1 server ip, column 2 ports to monitor).

The copy code is as follows:

# vim / scripts/ip-ports.txt

192.168.0.100 80

192.168.0.100 8081

192.168.0.101 8082

192.168.1.100 21

2. We can write a script to check whether the port is open in batch:

The copy code is as follows:

# vim / scripts/ncports.sh

#! / bin/bash

# check whether the server port is open. If it is successful, it will return a value of 0 to display ok, and if it fails, it will return a value of 1 to display fail

Cat / scripts/ip-ports.txt | while read line

Do

Nc-w 10-z $line > / dev/null 2 > & 1

If [$?-eq 0]

Then

Echo $line:ok

Else

Echo $line:fail

Fi

Done

3. Execute the script to view the running result as follows:

The copy code is as follows:

# chmod axix / scripts/ncports.sh

# / scripts/ncports.sh

192.168.0.100 80:ok

192.168.0.100 8081:ok

192.168.0.101 8082:ok

192.168.1.100 21:fail

3. Set an alarm when the port is blocked:

1. Email alarm:

1) first install the mail sender mutt under linux (see my other article, "how to send mail with the mutt command under linux"

2) modify the above ncports.sh detection script to add a line when displaying the failed fail:

...

Echo $line: fail

Echo "Server $line port is not available, please deal with it as soon as possible!" | | mutt-s "[computer room monitoring] server $line port is not available" test@139.com |

...

3) if the above receiving mailbox is set to Mobile 139 mailbox, and open to receive SMS notification, you can achieve the function of "SMS alarm".

2. Windows message pop-up window alarm:

(1) first open the pop-up window to receive messages and set the "messenger" service of the windows client to "start"

(2) use the smbclient command to send messages. The net script file is as follows:

The copy code is as follows:

# vim / scripts/net.sh

#! / bin/bash

# / scripts/net.sh

Case "$1" in

Send)

Echo "$3" | smbclient-I "$2"-m 'nmblookup-a "$2" | sed-e' 1d'-e'3 Greater Greater D` | cut-f2 | cut-d'-f1`

*)

Echo "usage:net send"

Exit 1

Esac

# chmod axix / scripts/net.sh

(3) send message pop-up command test: (sent to 192.168.1.83 this win xp machine, the content does not support Chinese)

The copy code is as follows:

# / scripts/net.sh send 192.168.1.83 "hello,nihao"

3. The script for sending email and message pop-up window alarm when the port is not available is as follows:

The copy code is as follows:

# vim / scripts/ncports.sh

#! / bin/bash

# check whether the server port is open. A value of 0 will be returned if it succeeds, and a value of 1 will be returned if you do not have a meeting.

Cat / scripts/ip-ports.txt | while read line

Do

Nc-w 10-z $line > / dev/null 2 > & 1

If [$?-eq 0]

Then

Echo $line:ok

Else

Echo $line:fail

Echo "Server $line port is not available, please deal with it as soon as possible!" | | mutt-s "[computer room monitoring] server $line port is not available" test@18.com |

/ scripts/net.sh send 192.168.1.83 "the $line fail"

Fi

Done

4. Join the task plan and execute it every 2 minutes.

The copy code is as follows:

# crontab-e

* / 2 * / scripts/ncports.sh > / dev/null 2 > & 1

# service crond restart

At this point, the study on "how to use the nc command to monitor the server port in Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report