#!/bin/ksh
# Shell Script:checklink.sh
# Function:auto Check the link of server IP and PORT
# date:2016.04.07
# modify:2016.04.07
# Copyright by Li Jian.
#根据系统类型的不同, loading system environment variables
If [' uname ' = ' Linux ']
Then
Profile=.bash_profile
Else
Profile=.profile
Fi. $HOME/$PROFILE
#在脚本执行目录下创建日志文件. Log and final result file for detection. dat
resultfile= $PWD/checklink.dat
log_file= $PWD/checklink.log
#判断脚本执行的时候传递的参数个数是否正确, including IP address and port number port two parameters
If [$#-ne 2]
Then
Echo ' date + '%y.%m.%d%T ' ' The parameter numbers are not correct,exit! ' >>${log_file}
Exit
Fi
#读取传递的脚本参数
Ip=$1
Port=$2
#判断两个参数是否为空
If [-Z $IP-O-Z $port]
Then
Echo ' date + '%y.%m.%d%T ' ' The parameter should not to null,exit! ' >>${log_file}
Exit
Fi
#通过Ping命令先检测指定的ip地址是否能连通, by pinging four packets (which can be sent by another number), limiting the timeout to 3 seconds (or other values, depending on how long the delay can endure), depending on the ratio of packets dropped to determine whether the IP address is connected, such as ping Baidu return ID results are as follows:
zxin10@ubuntu:~/lijian$ Ping Www.baidu.com-c 4-w 3
PING www.a.shifen.com (180.97.33.108) bytes of data.
Bytes from 180.97.33.108:icmp_seq=1 ttl=128 time=3.79 ms
Bytes from 180.97.33.108:icmp_seq=2 ttl=128 time=5.32 ms
Bytes from 180.97.33.108:icmp_seq=3 ttl=128 time=3.92 ms
---www.a.shifen.com ping statistics---
3 Packets transmitted, 3 received, 0% packet loss, time 2004ms
Rate= ' ping-c 4-w 3 $IP | grep ' Packet loss ' | Grep-v grep | Awk-f ', ' {print $} ' | Awk-f '% ' {print $} ' #得到丢包的比例的数值
#RATE = ' ping-c 4-w 3 $IP | grep ' Packet loss ' | Grep-v grep | cut-f3-d, | cut-f1-d% '
If [$RATE-eq] #这个值具体可以设置成75, 50, and so on, this is set in the case of total loss of packets is not pass
Then
Echo ' date + '%y.%m.%d%T ' ' Ping the $IP is not connected, failure ' >${log_file}
echo "Failure" #全丢了说明该IP地址不通
Else
Telnet $IP $port <<@@@ 2>${resultfile} 1>&2 #否则telnet命令检测指定端口号PORT是否连通, if you can connect, immediately quit exit the connection, will connect Results and error messages are written to the result file
Quit
@@@
Result= ' Cat ${resultfile} 2>/dev/null | grep "Connection Closed by foreign host" | Wc-l ' #匹配看结果文件中手否有这句话
Echo ' date + '%y.%m.%d%T ' ' "The result of Telnet is $RESULT" >${log_file}
If [$RESULT-eq 1]
Then
Echo ' date + '%y.%m.%d%T ' ' Telnet to the $IP and $port SUCCESS ' >>${log_file} #如果有的话就说明该端口是通的, returns the result string SUCCESS to the text Piece and print on the big screen at the same time
echo "SUCCESS"
Else
Echo ' date + '%y.%m.%d%T ' ' Telnet the $IP and $port failure ' >>${log_file} #否则就说明该端口是不通的, printing failure
echo "Failure"
Fi
Fi
Rm-f ${resultfile} #删除结果文件, log file retention
This is the script file checklink.sh, which is used to automatically detect the connectivity of the specified machine port.
Script execution permissions are given when running:
chmod +x checklnk.sh
Then execute the script and pass two parameters: the first IP address, the second port number, for example:
./checklink.sh 192.168.25.67 21
Question: Why not call the Telnet command directly to detect the specified IP address and port number, and call the ping command before calling the Telnet command?
Answer: For the telnet command, when the IP address is different, will be caught in a long wait, because Telnet can not set the timeout, so you need to ping the IP address through the screen to remove the IP, and then through Telnet to determine the specified port, If the port does not pass, Telnet will immediately return, if the short of the reliable can be connected, immediately quit exit Telnet Landing, based on the results of the return to determine whether connectivity.
Here is also a good post, through the C language program to set and control the return timeout time telnet, very classic
http://bbs.csdn.net/topics/340269286