ssh协议,网元设备循环登陆测试脚本
此脚本仅支持设备登陆测试,如有其他需求可根据此脚本进行修改。
脚本如下:
#!/bin/bash #网元设备登陆测试脚本(SSH协议),20210909 #iplist文件需要写为IPLIST=(IP1 IP2 IP3)格式,可用回车符分隔,如下例: #IPLIST=(IP1 #IP2 #IP3) #设置设备用户名以及密码 USER="user" PASS="passwd" DIR=`cd "$( dirname "$0" )" && pwd` source $DIR/iplist &> /dev/null DATA_YMDHM=`date +%Y%m%d%H%M` COUNTER=0 IPLISTSUM=${#IPLIST[@]} login_yes(){ /usr/bin/expect << EOF set timeout 20 spawn ssh ${USER}@${1} expect 'yes/no)? ' send "yes\r" expect "assword:" send "${PASS}\r" expect ">" send "quit\r" expect eof EOF } login_check(){ /usr/bin/expect << EOF set timeout 20 spawn ssh ${USER}@${1} expect "assword:" send "${PASS}\r" expect ">" send "quit\r" expect eof EOF } while true do ERR=0 GP=`cat ~/.ssh/known_hosts |grep ${IPLIST[$COUNTER]}|wc -l` if [ $GP -ne 0 ] ; then PUT="$(login_check ${IPLIST[$COUNTER]} )" else PUT="$(login_yes ${IPLIST[$COUNTER]})" fi #匹配写在一起会出现不匹配的情况,故此处分开写。 JG1=`echo $PUT | egrep '>' | wc -l` if [ $JG1 -ne 0 ];then echo "${IPLIST[$COUNTER]},ok!" >> put-${DATA_YMDHM}.log else ERR=$((ERR+1)) fi JG2=`echo $PUT | egrep '#' | wc -l` if [ $JG2 -ne 0 ];then echo "${IPLIST[$COUNTER]},ok!" >> put-${DATA_YMDHM}.log else ERR=$((ERR+1)) fi #根据上方匹配了多少次,这里写eq等于几 if [ $ERR -eq 2 ] ;then echo "${IPLIST[$COUNTER]},fail!" >> put-${DATA_YMDHM}.log fi COUNTER=$((COUNTER+1)) if [ $COUNTER -eq $IPLISTSUM ] ;then break fi done
发表评论