如何查看远端的服务是否开通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:
PORTSTATE SERVICE
80/tcp openhttp
Nmap finished: 1 IP address (1 host up) scanned in 0.417 seconds
如果写脚本通过nmap检查端口可以用下面的方法:
26PORT_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 5211.100.98.99 80 && echo ok
ok
[root@oldboy ~]# nc -w 108.8.8.8 53 && echo ok ||echo no
ok
如果写脚本通过nc检查端口并监控memcache服务可以用下面的方法:
exportoldboyTimestampMemcachedIp=$1
exportoldboyTimestampMemcachedPort=$2
exportoldboyTitle=NAGIOS
exportoldboyTimestampMD5=4ED06F8D41B9264OLD0BOY30BE5212BB7E34
exportwwwServerIp=$3
exportwwwServerPort=$4
printf"delete$oldboyTimestampMD5\r\n"|nc$oldboyTimestampMemcachedIp$oldboyTimestampMemcachedPort>/dev/null2>&1
sleep1
judge=($(printf"HEAD/oldboy/$oldboyTitleHTTP/1.1\r\nHost:$5\r\n\r\n"|nc$wwwServerIp$wwwServerPort|head-n1|tr"\r""\n"))
本文出自 “老男孩linux运维” 博客,请务必保留此出处http://oldboy.blog.51cto.com/2561410/942530