ping telnet ssh验证脚本

#!/bin/sh
###############################################################################################
#
#	Version :1.1
#
#	CREATE DATE : 2020-12-30
#	PLATFORM : Linux/AIX/HP-UX
#	USAGE : Ping Telnet SSH
#
###############################################################################################
start_time=$(date +%s)
export LC_ALL=zh_CN
export LANG=zh_C
path=`pwd`
iplist="${path}/iplist"
resultfile="/tmp/resultfile.tmp"
filetime=`date +'%Y%m%d%H%M%S'`
if [ ! -d "${path}/log/ssh/${filetime}" ]; then
        mkdir -p ${path}/log/ssh/${filetime}
fi
logfile="${path}/log/ssh/${filetime}/ssh.log"
pingok="${path}/log/ssh/${filetime}/ssh_pingok.log"
pingno="${path}/log/ssh/${filetime}/ssh_pingno.log"
telnetok="${path}/log/ssh/${filetime}/ssh_telnetok.log"
telnetno="${path}/log/ssh/${filetime}/ssh_telnetno.log"
sshok="${path}/log/ssh/${filetime}/ssh_sshok.log"
sshno="${path}/log/ssh/${filetime}/ssh_sshno.log"
###############################################################################################
function log
{	
	local logcontent=$*
	echo "[`date +"%Y-%m-%d %T"`]:${logcontent}"
}

function pingCheck
{
	local ip=$1
	if [ ! -z $ip ];then
		local rate=`ping -c 1 -w 3 $ip|grep 'packet loss'|grep -v grep|awk -F',' '{print $3}'|awk -F'%' '{print $1}'|awk '{print $NF}'`
        	if [ "${rate}" = "errors" ]; then
			rate=`ping -c 1 -w 3 $ip|grep 'packet loss'|grep -v grep|awk -F',' '{print $4}'|awk -F'%' '{print $1}'|awk '{print $NF}'`
		fi
		return "${rate}"
	fi

}

function telnetCheck
{
	local ip=$1
	local port=$2
	if [ ! -z $ip ];then
  		timeout 10 telnet $ip $port<<EOF 2>${resultfile} 1>&2
quit
EOF
	fi
}

function sshCheck
{
	local ip=$1
	local user=$2
	local password=$3
	if [ ! -z $ip ];then
		timeout 5 /usr/bin/sshpass -p "$password" ssh -t -t -o stricthostkeychecking=no  $user@$ip<<EOF 2>${resultfile} 1>&2
echo AAAAAA
exit
EOF
	fi
}
###############################################################################################

#default value
if [ $# -eq 0 ];then
        port=22
else
	if [ $# -gt 1 ];then
		log "The ssh's params is too many ,please input 1 parameter!"
		exit -1
        else
		port=$1
	fi
fi

if [ `uname`="Linux" ]
then
	profile=.bash_profile
else
	profile=.profile
fi

if [ -f $HOME/${profile} ]
then
	. $HOME/${profile}
fi

>${logfile}
>${pingok}
>${pingno}
>${telnetok}
>${telnetno}
>${sshok}
>${sshno}

#删除1天前的log文件
find ${path}/log/ssh -mtime +1 -name "*" | xargs -I {} rm -rf {}

#清空known_hosts
>${HOME}/.ssh/known_hosts

while read line
do
IP=`echo $line | awk '{print $1}'`
User=`echo $line | awk '{print $2}'`
Password=`echo $line | awk '{print $3}'`
#Ping	
pingCheck ${IP}
pingRet=$?
if [ "${pingRet}" = "100" ]; then
	log "===> ping ${IP},ping is NO!" |tee -a ${logfile}
	echo ${IP} >> ${pingno}
else
	log "===> ping ${IP},ping is OK!" |tee -a ${logfile}
	echo ${IP} >> ${pingok}	
#Telnet(ping OK 才执行telnet)
	telnetCheck $IP $port 
	telnet_result=`cat ${resultfile} 2>/dev/null |grep "Connection closed by foreign host"|wc -l`
	if [ $telnet_result -eq 1 ]
		then
		log "===> telnet ${IP} ${port},telnet is OK!"|tee -a ${logfile}
		echo ${IP} >> ${telnetok}
#SSH(ping OK and telnet OK 才执行SSH)
		sshCheck $IP $User $Password
		ssh_ok_result=`cat ${resultfile} 2>/dev/null |grep "AAAAAA"|wc -l`
		ssh_port_result=`cat ${resultfile} 2>/dev/null |grep "port 22"|wc -l`
		ssh_pwdno_result=`cat ${resultfile} 2>/dev/null |grep "please try again"|wc -l`
		ssh_pwd_expired_result=`cat ${resultfile} 2>/dev/null |grep "password has expired"|wc -l`
		ssh_known_hosts_result=`cat ${resultfile} 2>/dev/null |grep "no matching key exchange method found"|wc -l`
		ssh_nologin_result=`cat ${resultfile} 2>/dev/null |grep "This account is currently not available"|wc -l`
		if [ $ssh_ok_result -gt 0 ]
			then
			log "===> ssh ${IP},ssh is OK!"|tee -a ${logfile}
			echo ${IP} >> ${sshok}
		else
		
			if [ $ssh_port_result -gt 0 ]
				then
					log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
					echo ${IP}	22 端口不通 >> ${sshno}
			elif [ $ssh_pwdno_result -gt 0 ]
				then
					log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
					echo ${IP}	${User}:${Password}	用户或密码错误 >> ${sshno}
			elif [ $ssh_pwd_expired_result -gt 0 ]
				then
					log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
					echo ${IP}	${User}:${Password}	密码过期 >> ${sshno}
			elif [ $ssh_known_hosts_result -gt 0 ]
				then
					log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
					echo ${IP}	SSH 协议不兼容 >> ${sshno}
			elif [ $ssh_nologin_result -gt 0 ]
				then
					log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
					echo ${IP}	${User}:nologin 无登录权限 >> ${sshno}
			else
				log "===> ssh ${IP},ssh is NO!"|tee -a ${logfile}
				echo ${IP}	未知 >> ${sshno}
			fi
		fi
	else
		log "===> telnet ${IP} ${port},telnet is NO!"|tee -a ${logfile}
		echo ${IP} >> ${telnetno}
	fi
fi
done < $iplist

#sum data
ipSum=`cat $iplist|wc -l`
pingOKSum=`grep "ping is OK" ${logfile}|wc -l`
pingNOSum=`grep "ping is NO" ${logfile}|wc -l`
telnetOKSum=`grep "telnet is OK" ${logfile}|wc -l`
telnetNOSum=`grep "telnet is NO" ${logfile}|wc -l`
sshOKSum=`grep "ssh is OK" ${logfile}|wc -l`
sshNOSum=`grep "ssh is NO" ${logfile}|wc -l`
log "
+-------------------------------------------------------+
|ALL IP:${ipSum}						|
+-------------------------------------------------------+
|Ping	All : ${ipSum}	| OK : ${pingOKSum}	| NO : ${pingNOSum}	|
+-------------------------------------------------------+
|Telnet	All : ${pingOKSum}	| OK : ${telnetOKSum}	| NO : ${telnetNOSum}	|
+-------------------------------------------------------+
|SSH	All : ${telnetOKSum}	| OK : ${sshOKSum}	| NO : ${sshNOSum}	|
+-------------------------------------------------------+
" |tee -a ${logfile}
end_time=$(date +%s)
cost_time=$[ $end_time-$start_time ]
echo "END Time: $(($cost_time/60)) min $(($cost_time%60))s"
exit 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值