zoukankan      html  css  js  c++  java
  • 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 "    <<<<<===============================================>>>>>
    
    "
    # 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 "本月到期主机:
    "
    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 '
    ============================
    ';"
    #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="
    "
    
    for host in $hosts;
    do 
    let hostIndex+=1
    echo -e "主机:【$hostIndex】
    ┏==============================================┓
    "
    sshfindline $host
    echo -e "
    ┗==============================================┛
    
    "
    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 "下月到期主机:
    "
    #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 '
    ============================
    ';"
    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】
    ┏==============================================┓
    "
    
    sshfindline $host
    echo -e "
    ┗==============================================┛
    
    "
    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
    "
        echo -e "	SSH行号查找主机:传递行号,通过查找 ~/.ssh/config 匹配对应行;
    	输出前后行主机各配置项完整信息."
        echo -e "
    Usage:
    	sshfindline [line-number]"
    	echo -e "Example:
    	sshfindline 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
    
    
    

    本文来自博客园,作者:晴云孤魂,转载请注明原文链接:https://www.cnblogs.com/cnhack/p/14903592.html

  • 相关阅读:
    bzoj2301: [HAOI2011]Problem b懵逼乌斯反演
    bzoj3504: [Cqoi2014]危桥 网络流
    bzoj1588: [HNOI2002]营业额统计 splay瞎写
    bzoj1008快速面
    洛谷1212手动枚举各种情况(缩代码成瘾)
    bzoj1968真·想sha法bi题
    bzoj3674同上(好短)
    bzoj3673可持久化线段树实现可持久化数组实现可持久化并查集(好长)
    uoj98未来程序改 纯暴力不要想了
    bzoj3680模拟退火
  • 原文地址:https://www.cnblogs.com/cnhack/p/14903592.html
Copyright © 2011-2022 走看看