Linux教程

Linux查询SSH到期主机Shell/Bash版

本文主要是介绍Linux查询SSH到期主机Shell/Bash版,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#!/bin/bash 
#查看所有主机的到期时间,按到期时间顺序排列
echo -e "    查询所有主机到期时间,按日期顺序排列"
echo -e "    参数一可附加参数 (month/m) 查看当月到期主机详情;"
echo -e "    参数一可附加参数 (next/n) 查看下月到期主机详情;"
echo -e "    参数二可指定最多列出的主机,缺省为-1,即列出全部匹配主机;"
echo -e "    Example:ssh-expire-time m 或 ssh-expire-time n;"
echo -e "    Example:ssh-expire-time m 2 (匹配两次,只显示到期前两个)"
echo -e "    <<<<<===============================================>>>>>\n\n"
# if less than two arguments supplied, display usage
if [  $# -lt 1 ]
then
    cat ~/.ssh/config|grep 到期时间|sort
    exit 1
fi

##至多列出的主机个数
if [ ! -z "$2" ];then
grepCount=`expr $2 + 0`
else
grepCount=-1
fi


if [[ "$1" == "month" || "$1" == "m" ]];then
nowMonth=$(date +"%Y-%m")
echo -e "本月到期主机:\n"
hostIndex=0
#cat ~/.ssh/config|grep 到期时间|sort|grep -m $grepCount $nowMonth|sed -r 's/^.*('$nowMonth'.*)$/\1/ig'|\
#xargs -n 1 -i sh -c "echo -e '主机 $hostIndex';eval sshfindtime {};echo -e '\n============================\n';"
#hosts=`cat ~/.ssh/config|grep 到期时间|sort|grep -m $grepCount $nowMonth|sed -r 's/^.*('$nowMonth'.*)$/\1/ig'`;
#下述把时分秒替换掉,不关心
#hosts=`cat ~/.ssh/config|grep 到期时间|sort|grep -m $grepCount $nowMonth|sed -r -e 's/^.*('$nowMonth'.*)$/\1/ig' -e 's/[0-9]{1,2}:.*$//ig'`;

##下为新方式,最终改用 sshfindline 查找,适配多主机同一天到期
hosts=`grep -n '' ~/.ssh/config|grep 到期时间|sort -t ':' -k2|grep $nowMonth|cut -d ':' -f1|grep -m $grepCount ''`
#OLD_IFS=$IFS
#IFS="\n\b"

for host in $hosts;
do 
let hostIndex+=1
echo -e "主机:【$hostIndex】\n┏==============================================┓\n"
sshfindline $host
echo -e "\n┗==============================================┛\n\n"
done
#IFS=$OLD_IFS
if [ $grepCount -eq -1 -o $hostIndex -lt $grepCount ];then
echo "共找到 $hostIndex 个主机"
fi
exit 0
fi

if [[ "$1" == "next" || "$1" == "n" ]];then
month=`date +"%m"`
##月份跨年处理
if [ $month -eq 12 ];then
	month=0
	year=`date +"%Y"`
	Nextmonth=$(expr $year + 1 )"\-0?"$(expr $month + 1 )"[^0-9]+"
else
	Nextmonth=`date +"%Y\-0?"`$(expr $month + 1 )"[^0-9]+"
fi

echo -e "下月到期主机:\n"
#cat ~/.ssh/config|grep 到期时间|sort|grep -m $grepCount $Nextmonth|sed -r 's/^.*('$Nextmonth'.*)$/\1/ig'|\
#xargs -n 1 -i sh -c "eval sshfindtime {};echo -e '\n============================\n';"
hostIndex=0

##下为原始方式,不能区分同一天多台主机到期的情况
hosts=`cat ~/.ssh/config|grep 到期时间|sort|grep -m $grepCount $Nextmonth|sed -r -e 's/^.*('$Nextmonth'.*)$/\1/ig' -e 's/[0-9]{1,2}:.*$//ig'`;

##下为新方式,最终改用 sshfindline 查找,适配多主机同一天到期
hosts=`grep -n '' ~/.ssh/config|grep 到期时间|sort -t ':' -k2|grep -E $Nextmonth|cut -d ':' -f1|grep -m $grepCount ''`

for host in $hosts;
do 
let hostIndex+=1
echo -e "主机:【$hostIndex】\n┏==============================================┓\n"

sshfindline $host
echo -e "\n┗==============================================┛\n\n"
done
if [ $grepCount -eq -1 -o $hostIndex -lt $grepCount ];then
echo "共找到 $hostIndex 个主机"
fi
exit 0
fi


附: 依赖脚本sshfindline:

#!/bin/bash 
SCRIPTPATH=$(realpath $0)
#SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
#SCRIPTPATH=$(dirname $(readlink -f "$0"))

display_usage() {
	echo -e "$SCRIPTPATH\n"
    echo -e "\tSSH行号查找主机:传递行号,通过查找 ~/.ssh/config 匹配对应行;\n\t输出前后行主机各配置项完整信息."
    echo -e "\nUsage:\n\tsshfindline [line-number]"
	echo -e "Example:\n\tsshfindline 105"
}
# if less than two arguments supplied, display usage
if [  $# -lt 1 ]
then
    display_usage
    exit 1
fi

# check whether user had supplied -h or --help . If yes display usage
if [[ ( $* == "--help") ||  $* == "-h" ]]
then
    display_usage
    exit 0
fi

linenumber=$1

#给文件每一行加上行号:
#grep -n '' ~/.ssh/config

hostStartLine=$(grep -n '' ~/.ssh/config|tac|tail -n $linenumber|grep -iE -m 1 ':Host '|cut -d ':' -f1)
hostAlias=$(sed -n "${hostStartLine}p" ~/.ssh/config)
echo $hostAlias
sed -nr $hostStartLine',/Host /{/Host /b;p}' ~/.ssh/config


这篇关于Linux查询SSH到期主机Shell/Bash版的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!