查看远端的端口是否通畅3个简单实用案例!
如何查看远端的服务是否开通tcp 8000端口,比如,查看etiantian.org是否开通tcp 8000端口?
以80端口为例
法一:此法常被用来检测是个远端端口是否通畅。
[root@oldboy ~]# 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检查端口可以用下面的方法:
[root@oldboy ~]# echo -e "\n"|telnet baidu.com 80|grep Connected
Connection closed by foreign host.
Connected to baidu.com (123.125.114.144).
法二:通过nmap来检查端口是否通畅
[root@oldboy ~]# nmap etiantian.org -p 80
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检查端口可以用下面的方法:
26 PORT_COUNT=`nmap $ip_add -p $port|grep open|wc -l`
29 [[ $PORT_COUNT -ge 1 ]] && echo "$ip_add $port is ok." || echo "$ip_add $port is unknown."
法三:通过nc命令检查
[root@oldboy ~]# nc -w 5 211.100.98.99 80 && echo ok
ok
[root@oldboy ~]# nc -w 10 8.8.8.8 53 && echo ok ||echo no
ok
如果写脚本通过nc检查端口并监控memcache服务可以用下面的方法:
export oldboyTimestampMemcachedIp=$1
export oldboyTimestampMemcachedPort=$2
export oldboyTitle=NAGIOS
export oldboyTimestampMD5=4ED06F8D41B9264OLD0BOY30BE5212BB7E34
export wwwServerIp=$3
export wwwServerPort=$4
printf "delete $oldboyTimestampMD5\r\n" | nc $oldboyTimestampMemcachedIp $oldboyTimestampMemcachedPort >/dev/null 2>&1
sleep 1
judge=($(printf "HEAD /oldboy/$oldboyTitle HTTP/1.1\r\nHost: $5\r\n\r\n" | nc $wwwServerIp $wwwServerPort | head -n1|tr "\r" "\n"))
后记,其实还有很多方法,例如通过nagios的插件check_tcp直接操作等等。