Linux查詢SSH到期主機Shell/Bash版
阿新 • • 發佈:2021-06-19
#!/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