1. 程式人生 > >linux 檢測遠端埠是否開啟

linux 檢測遠端埠是否開啟

1. telnet

> telnet [host] [port]  # 檢測指定埠是否通暢。
> telnet baidu.com 80
Trying 123.125.114.144...
Connected to baidu.com (123.125.114.144).  # 出現Connected表示連通了,說明百度的80埠開放的
Escape character is '^]'. # 按鍵ctrl+]退出此地。
^]
telnet> quit
Connection closed.

# 如果寫指令碼通過telnet檢查埠可以用下面的方法:
> echo -e "\n"
|telnet baidu.com 80|grep Connected Connection closed by foreign host. Connected to baidu.com (123.125.114.144).

2. nmap

# nmap需要安裝  sudo apt install nmap
nmap [ip] [-p port]  # 測試埠
nmap [ip]  # 顯示全部開啟的埠,根據顯示close/open確定埠是否開啟
> nmap etiantian.org -p 80  # 通過nmap來檢查埠是否通暢
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2012
-07-22 17:22 PDT Interesting ports on 211.100.98.99: PORT STATE SERVICE 80/tcp open http Nmap finished: 1 IP address (1 host up) scanned in 0.417 seconds # 如果寫指令碼通過nmap檢查埠可以用下面的方法: PORT_COUNT=`nmap $ip_add -p $port|grep open|wc -l` [[ $PORT_COUNT -ge 1 ]] && echo "$ip_add $port is ok." || echo
"$ip_add $port is unknown."

3. nc

# 通過nc命令檢查
nc -v [host] [port]
埠未開啟返回狀態為非0 

# 指令碼方式
> nc -w 5  211.100.98.99 80 && echo ok
ok
> nc -w 10  8.8.8.8 53 && echo ok ||echo no  
ok

參考文章:
http://www.cnblogs.com/onmyway20xx/articles/3626433.html
http://blog.chinaunix.net/uid-26000296-id-4086980.html
https://www.cnblogs.com/pizitai/p/6519872.html