shell圖形化界面腳本實現(xiàn)

image.png
image.png
語法:
whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>

whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .

進度條:
whiptail --gauge "<test to show>" <height> <width> <inital percent>

1.基于架構(gòu)/角色進行檢測2.檢測網(wǎng)絡(luò)端口映射是否正常3.檢測其進程/啟動。

shell字符串拼接截取,shell數(shù)組如何引入函數(shù),shell圖像化界面,以及一些檢測和啟動服務(wù)的命令

#!/bin/bash
. /etc/rc.d/init.d/functions

#此腳本用于---服務(wù)運維
#time:2018/10/29
#@一個熱愛生活的好少年_lzy

echo_red() {
    stty erase '^H'
    echo -n -e "\033[31m$1\033[0m"
}

echo_red_enter() {
    echo -e "\033[31m$1\033[0m"
}

echo_green() {
    stty erase '^H'
    echo -n -e "\033[32m$1\033[0m"
}

echo_green_enter() {
    echo -e "\033[32m$1\033[0m"
}

function clean_buffer(){

    echo 3 > /proc/sys/vm/drop_caches
}

function warning
{
    if [[ $# = 2 ]];then
        [[ "$1" = 0 ]] && action "$2" /bin/true || action "$2" /bin/false
    elif [[ $# = 1 ]];then
        icontent=$(cat $ERROR_LOG)
        if [[ -n "$icontent" ]];then
            action "$1" /bin/false && cat $ERROR_LOG >> $LOG && rm -rf $ERROR_LOG && exit
        else
            action "$1" /bin/true
        fi
    fi
}

#進程
#定義全局變量
hybird_select_animo=
#界面2
function radiolist() {
    DISTROS=$(whiptail --title "請選擇所需要的模塊" --radiolist \
    "請選擇所需要的腳本?" 15 60 4 \
    "hybird0" "/與網(wǎng)絡(luò)檢測" ON \
    "hybird1" "中架構(gòu)" OFF \
    "hybird2" "架構(gòu)" OFF \
    "hybird3" "他()" OFF 3>&1 1>&2 2>&3)
 
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        hybird_select_animo=$DISTROS
        echo_green_enter "您所選擇的模塊 $hybird_select_animo" 
    else
        echo "You chose Cancel."
    fi
}

#界面3
function hybird0 {
    OPTION=$(whiptail --title "請選擇需要檢測的模塊" --menu "Choose your option" 15 60 4 \
    "1" "網(wǎng)絡(luò)聯(lián)通性)" \
    "2" "t服務(wù)檢測" \
    "3" "s服務(wù)器檢測" \
    "4" "all(融合)"  3>&1 1>&2 2>&3)
 
    exitstatus=$?
    #echo > $hytmp
    #shell數(shù)組如何傳遞到函數(shù)中
    if [ $exitstatus = 0 ]; then
        #echo "Your chosen option:" $OPTION
        if [ $OPTION == 1 ];then
            echo_green_enter "網(wǎng)絡(luò)聯(lián)通性檢測:"
            #network_test
            network_test0
        elif [ $OPTION == 2 ];then
            echo "開始檢服務(wù):"
           # Hardware 變量融t
          #Process_test $Process_all_rh  
            echo_green "本次共檢測nginx rsync memcached 服務(wù)"
            Process_all_rh=("nginx" "rsync" "memcached")
            Process_test "${Process_all_rh[*]}"
            Process_all_rh1=("telt" "tele" "ant")
            Process_test_java "${Process_all_rh1[*]}"
            #swift_service_test    
        elif [ $OPTION == 3 ];then
            swift_service_test   
        elif [ $OPTION == 4 ];then
            echo_green_enter "----性能測試:"
            all
            
        fi
    else
        echo "You chose Cancel."
    fi  
}






#swift進程檢測

function swift_service_test(){
    echo_red "swift進程檢測:"
    ps -ef | grep swift
    sleep 5
    if (whiptail --title "是否繼續(xù)啟動swift服務(wù)" --yesno "建議:相關(guān)存儲服務(wù)服務(wù) 是否繼續(xù)執(zhí)行:請選擇" 15 80) then
    echo "You chose Yes. 開始執(zhí)行環(huán)境檢測!"  

    swift_proxy restart




else
    echo "You chose No. Exit status was $?."
fi

}



function swift_proxy() {
    systemctl $1 openstack-swift-proxy.service memcached.service rsyncd.service openstack-keystone.service
    systemctl $1 openstack-swift-account.service openstack-swift-account-auditor.service openstack-swift-account-reaper.service openstack-swift-account-replicator.service
    systemctl $1 openstack-swift-container.service openstack-swift-container-auditor.service openstack-swift-container-replicator.service openstack-swift-container-updater.service
    systemctl $1 openstack-swift-object.service openstack-swift-object-auditor.service openstack-swift-object-replicator.service openstack-swift-object-updater.service
}

function swift_object() {
    systemctl $1 rsyncd.service
    systemctl $1 openstack-swift-account.service openstack-swift-account-auditor.service openstack-swift-account-reaper.service openstack-swift-account-replicator.service
    systemctl $1 openstack-swift-container.service openstack-swift-container-auditor.service openstack-swift-container-replicator.service openstack-swift-container-updater.service
    systemctl $1 openstack-swift-object.service openstack-swift-object-auditor.service openstack-swift-object-replicator.service openstack-swift-object-updater.service
}















# #查看telnet命令是否存在
function network_test0 {
    #查看telnet命令是否存在
    which "telnet" > /dev/null
    if [ $? -eq 0 ]
    then
        echo_green_enter "telnet is exist"
        network_telnet
    else
        echo_red "telnet not exist"
        echo "請先安裝telnet服務(wù):yum install  -y telnet 即可。"
    fi  

}

#網(wǎng)絡(luò)層面檢測
function network_telnet {
    #如果telnet安裝成功

    #shell數(shù)組
    array=(www.baidu.com)
    eco_print1="檢測結(jié)果:該服務(wù)器連接"
    eco_print3="主站服務(wù)器正常ok"
    eco_print4="主站服務(wù)器異常:請盡快開通。"
    #遍歷數(shù)組
    for(( i=0;i<${#array[@]};i++)) 
        do 
        #echo ${array[i]}; 
        NPORT1=`echo ""|telnet  ${array[i]} 443 2>/dev/null|grep "\^]"|wc -l`
        if [[ $NPORT1 == 1  ]];then
            eco_print=${eco_print1}${array[i]}${eco_print3}
            echo_green ${eco_print}
        else
            eco_print=${eco_print1}${array[i]}${eco_print4}
            echo_red ${eco_print}
            whiptail --title "警告" --msgbox ${eco_print} 10 60
        fi
        sleep 1
        echo " "

    done;


}


#服務(wù)進程檢測



#變量
eco_Pro_print1="檢測結(jié)果:該服務(wù)"
eco_Pro_print3="已啟動正常ok"
eco_Pro_print4="異常未啟動(并檢測服務(wù)器是否部署該服務(wù))"

#function Arrayassignment(){

#}



#將所傳的服務(wù)進行進程檢測
function Process_test() {
    arr=$1
    #輪詢上述所傳的數(shù)組;
    for i in ${arr[*]}; do
        echo $i
        #echo ${array[i]}; 
        A=`ps -C $i --no-header |wc -l`
        echo_green "正在查看服務(wù)進程是否存在:"
        ps -ef | grep $i
        sleep 3
        if [ $A -eq 0 ];then                               ## 如果沒有進程值得為 零 服務(wù)未啟動 
            #彈出是否重啟服務(wù)界面
            #if nginx如何下  teleport(java服務(wù))需要/etc/ini.d/teleport start
            #
            #if 是rsync的話 需要變更為rsyncd
            echo_red "目前無該服務(wù)進程"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

            else
                value=$i
            fi
            
            echo $value
            restart_Process_Interface $value
        
            
        else
            echo_green "該服務(wù)正常:進程已啟動----------------"

        fi
    done

}

function Process_test_java() {
    arr=$1
    #輪詢上述所傳的數(shù)組;
    for i in ${arr[*]}; do
        echo $i
        #echo ${array[i]}; 
        #A=`ps -C $i --no-header |wc -l`
        echo_green "正在查看服務(wù)進程是否存在:"
        ps -ef | grep $i

        if [[ $i == teleport ]]; then

            #statements
            Pvalue=${i}"/"
        elif [[ $i == holmes ]]; then
            #statements
            Pvalue=${i}"/"
        else
            Pvalue=$i
        fi



        A=`ps -ef | grep $Pvalue | wc -l`
        sleep 3
        if [ $A -eq 1 ];then                               ## 如果沒有進程值得為 零 服務(wù)未啟動 
            #彈出是否重啟服務(wù)界面
            #if nginx如何下  (java服務(wù))需要/etc/ini.d/
            #
            #if 是rsync的話 需要變更為rsyncd
            echo_red "目前無該服務(wù)進程:"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

            else
                value=$i
            fi
            
            echo $value
            restart_Process_Interface_java $value
        
            
        else
            echo_green "該服務(wù)正常:進程已啟動----------------"

        fi
    done

}


#是否重啟服務(wù)
function restart_Process(){

    if (whiptail --title "歡迎使用維護腳本" --yesno "提示:是否啟動該服務(wù):請選擇" 15 80) then
    echo "You chose Yes. 正在啟動服務(wù)!"  

    #radiolist
    if [[ $hybird_select_animo == hybird0 ]]; then
        hybird0
        #statements:
    elif [[ $hybird_select_animo == hybird1 ]]; then
        #statements
        hybird1
    elif [[ $hybird_select_animo == hybird2 ]]; then
     #statements
        hybird2
    elif [[ $hybird_select_animo == hybird3 ]]; then
        #statements
        hybird3
    fi



else
    echo "You chose No. Exit status was $?."
fi
}

#變量 shell 字符串拼接/截?。?eco_Pro_Interfac_print1="是否重啟該服務(wù):"
eco_Pro_Interfac_print3="已啟動正常ok"
eco_Pro_Interfac_print4=":服務(wù)器異常啟動失?。úz測服務(wù)器是否部署該服務(wù))"

#界面顯示并抉擇是否重啟/并檢測(nginx/rabbit/rsyslog/rsyncd/)
function restart_Process_Interface(){

    restart_Process_name=$1
    eco_Pro_Interfac_print=${eco_Pro_Interfac_print1}${restart_Process_name}
    if (whiptail --title $eco_Pro_Interfac_print --yesno "提示:        是否繼續(xù)執(zhí)行:請選擇" 15 80) then
        echo "You chose Yes. 開始啟動服務(wù)!"  
        systemctl  start  $restart_Process_name

        if [[ $restart_Process_name == rsyncd ]]; then
            #statements
            restart_Process_name=rsync
        fi

                #如何判斷進程是否存在
        A=`ps -C $restart_Process_name --no-header |wc -l`
        if [ $A -eq 0 ];then                               ## 如果沒有進程值得為 零 服務(wù)未啟動 
            #
            eco_Pro_Interfac_print_error=${restart_Process_name}${eco_Pro_Interfac_print4}
            echo_red $eco_Pro_Interfac_print_error
                    
        else
            eco_Pro_Interfac_print_suc=${restart_Process_name}${eco_Pro_Interfac_print3}
            echo_green $eco_Pro_Interfac_print_suc

         fi

    else
        echo "You chose No. Exit status was $?."
    fi
}

function restart_Process_Interface_java(){

    restart_Process_name=$1
    eco_Pro_Interfac_print=${eco_Pro_Interfac_print1}${restart_Process_name}
    if (whiptail --title $eco_Pro_Interfac_print --yesno "提示:        是否繼續(xù)執(zhí)行:請選擇" 15 80) then
        echo "You chose Yes. 開始啟動服務(wù)!"  
        cd /etc/init.d
        ./$restart_Process_name start

        if [[ $restart_Process_name == rsyncd ]]; then
            #statements
            restart_Process_name=rsync
        fi


        A=`ps -ef | grep $Pvalue | wc -l`
        if [ $A -eq 1 ];then                               ## 如果沒有進程值得為 零 服務(wù)未啟動 
            #如何進行字符串拼接
            eco_Pro_Interfac_print_error=${restart_Process_name}${eco_Pro_Interfac_print4}
            echo_red $eco_Pro_Interfac_print_error
                    
        else
            eco_Pro_Interfac_print_suc=${restart_Process_name}${eco_Pro_Interfac_print3}
            echo_green $eco_Pro_Interfac_print_suc

         fi

    else
        echo "You chose No. Exit status was $?."
    fi
}














#執(zhí)行入口圖形化界面
if (whiptail --title "歡迎使用---維護腳本" --yesno "提示:是否繼續(xù)執(zhí)行:請選擇" 15 80) then
    echo "You chose Yes. 開始執(zhí)行環(huán)境檢測!"  

    radiolist
    if [[ $hybird_select_animo == hybird0 ]]; then
        hybird0
        #statements
    elif [[ $hybird_select_animo == hybird1 ]]; then
        #statements
        hybird1
    elif [[ $hybird_select_animo == hybird2 ]]; then
     #statements
        hybird2
    elif [[ $hybird_select_animo == hybird3 ]]; then
        #statements
        hybird3
    fi



else
    echo "You chose No. Exit status was $?."
fi
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,715評論 0 5
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,335評論 0 10
  • 請問,傻子會不會傷心,做為一個傻子,你忽視愛你的人,你把父母最親近最真摯的愛當(dāng)成麻煩,拿出你的所有去愛那個虛幻,我...
    PlscallmeMrtunk閱讀 879評論 0 1
  • 感恩!感恩郝現(xiàn)高書記理解支持我的工作,感恩施工單位全力配合。謝謝!謝謝!謝謝! 感恩!騰讓消防通道,停車位減少,誤...
    梧桐70閱讀 331評論 0 0

友情鏈接更多精彩內(nèi)容