Linux学习之查看远端的端口是否通畅

以80端口为例解答

法一:此法常被用来检测是个远端端口是否通畅

1 [root@oldboy ~]# telnet baidu.com 80
2 Trying 123.125.114.144...
3 Connected to baidu.com (123.125.114.144). #==>出现Connected表示连通了,说明百度的80端口开放的
4 Escape character is '^]'. #==>ctrl+]退出此地。
5 ^]
6 telnet> quit
7 Connection closed.

如果写脚本通过telnet检查端口可以用下面的方法:

1 [root@oldboy ~]# echo -e "\n"|telnet baidu.com 80|grep Connected
2 Connection closed by foreign host.
3 Connected to baidu.com (123.125.114.144).

法二:通过nmap来检查端口是否通畅

1 [root@oldboy ~]# nmap etiantian.org -p 80            
2 Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2012-07-22 17:22 PDT
3 Interesting ports on 211.100.98.99:
4 PORT   STATE SERVICE
5 80/tcp open  http
6 Nmap finished: 1 IP address (1 host up) scanned in 0.417 seconds

如果写脚本通过nmap检查端口可以用下面的方法:

1 26  PORT_COUNT=`nmap $ip_add  -p $port|grep open|wc -l`
2 29  [[ $PORT_COUNT -ge 1 ]] && echo "$ip_add $port is ok." || echo "$ip_add $port is unknown."

法三:通过nc命令检查

1 [root@oldboy ~]# nc -w 5  211.100.98.99 80 && echo ok
2 ok
3 [root@oldboy ~]# nc -w 10  8.8.8.8 53 && echo ok ||echo no  
4 ok

如果写脚本通过nc检查端口并监控memcache服务可以用下面的方法:

 1 export oldboyTimestampMemcachedIp=$1
 2 export oldboyTimestampMemcachedPort=$2
 3 export oldboyTitle=NAGIOS
 4 export oldboyTimestampMD5=4ED06F8D41B9264OLD0BOY30BE5212BB7E34
 5 export wwwServerIp=$3
 6 export wwwServerPort=$4
 7 
 8 printf "delete $oldboyTimestampMD5\r\n" | nc $oldboyTimestampMemcachedIp $oldboyTimestampMemcachedPort >/dev/null 2>&1
 9 sleep 1
10 judge=($(printf "HEAD /oldboy/$oldboyTitle HTTP/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

posted @ 2016-01-21 17:42  天~宇~翱~翔  阅读(2112)  评论(0编辑  收藏  举报