#!/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
ping telnet ssh验证脚本
最新推荐文章于 2022-11-26 10:36:31 发布