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圖像化界面,以及一些檢測和啟動服務的命令

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

#此腳本用于---服務運維
#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服務檢測" \
    "3" "s服務器檢測" \
    "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 "開始檢服務:"
           # Hardware 變量融t
          #Process_test $Process_all_rh  
            echo_green "本次共檢測nginx rsync memcached 服務"
            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服務" --yesno "建議:相關(guān)存儲服務服務 是否繼續(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服務:yum install  -y telnet 即可港准。"
    fi  

}

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

    #shell數(shù)組
    array=(www.baidu.com)
    eco_print1="檢測結(jié)果:該服務器連接"
    eco_print3="主站服務器正常ok"
    eco_print4="主站服務器異常:請盡快開通。"
    #遍歷數(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;


}


#服務進程檢測



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

#function Arrayassignment(){

#}



#將所傳的服務進行進程檢測
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 "正在查看服務進程是否存在:"
        ps -ef | grep $i
        sleep 3
        if [ $A -eq 0 ];then                               ## 如果沒有進程值得為 零 服務未啟動 
            #彈出是否重啟服務界面
            #if nginx如何下  teleport(java服務)需要/etc/ini.d/teleport start
            #
            #if 是rsync的話 需要變更為rsyncd
            echo_red "目前無該服務進程"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

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

        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 "正在查看服務進程是否存在:"
        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                               ## 如果沒有進程值得為 零 服務未啟動 
            #彈出是否重啟服務界面
            #if nginx如何下  (java服務)需要/etc/ini.d/
            #
            #if 是rsync的話 需要變更為rsyncd
            echo_red "目前無該服務進程:"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

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

        fi
    done

}


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

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

    #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="是否重啟該服務:"
eco_Pro_Interfac_print3="已啟動正常ok"
eco_Pro_Interfac_print4=":服務器異常啟動失斄粕肌(并檢測服務器是否部署該服務)"

#界面顯示并抉擇是否重啟/并檢測(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. 開始啟動服務!"  
        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                               ## 如果沒有進程值得為 零 服務未啟動 
            #
            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. 開始啟動服務!"  
        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                               ## 如果沒有進程值得為 零 服務未啟動 
            #如何進行字符串拼接
            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)系作者
  • 序言:七十年代末梢什,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子朝聋,更是在濱河造成了極大的恐慌嗡午,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冀痕,死亡現(xiàn)場離奇詭異荔睹,居然都是意外死亡,警方通過查閱死者的電腦和手機言蛇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門僻他,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人腊尚,你說我怎么就攤上這事吨拗。” “怎么了婿斥?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵劝篷,是天一觀的道長。 經(jīng)常有香客問我民宿,道長娇妓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任活鹰,我火速辦了婚禮哈恰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘志群。我一直安慰自己蕊蝗,他們只是感情好,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布赖舟。 她就那樣靜靜地躺著蓬戚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪宾抓。 梳的紋絲不亂的頭發(fā)上子漩,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機與錄音石洗,去河邊找鬼幢泼。 笑死,一個胖子當著我的面吹牛讲衫,可吹牛的內(nèi)容都是我干的缕棵。 我是一名探鬼主播孵班,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼招驴!你這毒婦竟也來了篙程?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤别厘,失蹤者是張志新(化名)和其女友劉穎虱饿,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體触趴,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡氮发,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了冗懦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爽冕。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖披蕉,靈堂內(nèi)的尸體忽然破棺而出颈畸,到底是詐尸還是另有隱情,我是刑警寧澤嚣艇,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布承冰,位于F島的核電站华弓,受9級特大地震影響食零,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜寂屏,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一贰谣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧迁霎,春花似錦吱抚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至昌粤,卻和暖如春既绕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背涮坐。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工凄贩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人袱讹。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓疲扎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子椒丧,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

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