緣起
檢測(cè)遠(yuǎn)程服務(wù)器的某一個(gè)端口(尤其是 tcp )是否已經(jīng)被打開(kāi),這貌似是 SA 們排錯(cuò)時(shí)遇到的一個(gè)常見(jiàn)場(chǎng)景叮姑,
方案
telnet
最早踢代,我常用的方案是使用 telnet。估計(jì)那時(shí)也只有 telnet 可用:)
yum -y install telnet;
telnet baidu.com 80;
上面的命令正確連上以后會(huì)有輸出:
Trying 180.149.132.47...
Connected to baidu.com.
Escape character is '^]'.
nc
后來(lái)纫版,發(fā)現(xiàn)還有 nc,網(wǎng)絡(luò)工具中的瑞士軍刀:)
yum -y install nc;
nc -vz baidu.com 80;
成功后會(huì)提示:
Connection to baidu.com 80 port [tcp/http] succeeded!
仔細(xì)看上面的這句輸出提示土全,看出來(lái)什么名堂沒(méi)有捎琐?對(duì)啦:“tcp/http”会涎!這么說(shuō) nc 還能檢測(cè) udp 端口不成裹匙?man 了一下,還真可以:
nc -vz -u 10.0.0.1 53
成功以后系統(tǒng)提示:
Connection to 10.0.0.1 53 port [udp/domain] succeeded!
bash
再后來(lái)末秃,中老年如我概页,終于返璞歸真,發(fā)現(xiàn)其實(shí) bash 就直接支持這種檢測(cè)练慕。OK惰匙,廢話少說(shuō),直接看命令:
man bash
找到這幾句提示:
/dev/tcp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket. /dev/udp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a UDP connection to the corresponding socket.
故而測(cè)試命令很好玩兒:
>/dev/tcp/baidu.co/80;echo $?;
當(dāng)成功連上 baidu.com 的 tcp 80 端口的時(shí)候铃将,返回 0项鬼。