dialog 使用案例

#!/bin/bash

#全局变量
IP=""
Netmask=""
Gateway=""
DNS1=""
DNS2=""
DNS3=""
NETWORKFILE="/etc/sysconfig/network-scripts/ifcfg-ens32"
networkarray=()

trapper() {
     trap ':' INT  EXIT TSTP TERM HUP  #<==定义需要屏蔽的信号,冒号表示啥都不做。
}


Menu(){
    OPTION=$(dialog --title "www.yuntongxun.com" --menu "Please Choose" 15 60 5 \
    1 "NetWork Config" \
    2 "Server Info" \
    3 "Auth"  3>&1 1>&2 2>&3 )
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        # echo "Your favorite programming language is:" $OPTION
           case $OPTION in
            1)
                NetworkModify

            ;;
            2)
                Serversinfo
            ;;
            3)
             Auth

            ;;
            *)
                echo "not found"
            ;;
            esac
    fi

}
Serversinfo(){
     OPTION=$(dialog --title "Server info" --menu "Choose >>:" 15 60 4 \
    1 "Network Info" \
    2 "Activate service address view" \
    3 "Activation service management"  3>&1 1>&2 2>&3 )
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        # echo "Your favorite programming language is:" $OPTION
           case $OPTION in
            1)
              ShowNetworkinfo
            ;;
            2)
                activateServerinfo
            ;;
            3)
              activateServermge
            ;;
            *)
                echo "not found"
            ;;
            esac
    else
        echo "You chose Cancel."
    fi

}

GetNetworkinfo(){
     IP=$(egrep "IPADDR" $NETWORKFILE | awk -F= '{print $2}')
     Netmask=$(egrep "NETMASK" $NETWORKFILE | awk -F= '{print $2}')
     Geteway=$(egrep "GATEWAY" $NETWORKFILE | awk -F= '{print $2}')
     DNS1=$( egrep "^DNS1" $NETWORKFILE | awk -F= '{print $2}')
    DNS2=$( egrep "^DNS2" $NETWORKFILE | awk -F= '{print $2}')
    DNS3=$( egrep "^DNS3" $NETWORKFILE | awk -F= '{print $2}')
}

ShowNetworkinfo(){
      GetNetworkinfo
     dialog  --title  "Networkinfo"   --msgbox "IP:$IP\nNetmask:$Netmask\nGeteway:$Geteway\nDNS1:$DNS1\nDNS2:$DNS2\nDNS3:$DNS3" 13 60
     if [[ $? -eq 0 ]];then
             Serversinfo
      else
            Serversinfo
      fi
}

IP_CheckContinue(){
    Message=$1
    if [[ $IPFlag -eq -1 ]];then
           dialog --title "Address Check" --no-shadow --yesno "$Message Input Error\nYes Continue to configure\nNo Back to main menu" 10 60
            if [[ $? -eq 0 ]];then
                NetworkModify
            else
                  Menu
            fi
    fi
}

##网络配置
NetworkModify(){
  GetNetworkinfo
  network=$(dialog --title "NetworkConfig" --form "Please input  Address:" 15 60 6  \
  "*IP:"		 1  1 "${IP}" 1  15  15  0  \
  "*Netmask:" 2  1 "${Netmask}" 2  15    15  0  \
  "*Gateway:" 3  1 "${Geteway}" 3  15   15  0  \
  "DNS1:"    4  1 "${DNS1}" 4  15  15    0  \
  "DNS2:" 	 5  1 "${DNS2}" 5  15  15    0  \
  "DNS3:"    6  1 "${DNS3}" 6  15  15    0   3>&1 1>&2 2>&3)
   exitstatus=$?
   if [ $exitstatus = 0 ]; then
          echo  $network
          networkarray=()
          index=0
          for i in ${network[*]}
          do
             networkarray[$index]=$i
             index=$(( $index + 1 ))
          done
          #最后一次
          index=$(( $index - 1 ))

          if [[  $index -lt 5 ]];then
                dialog    --title   "Input Error"   --msgbox  "The IP subnet mask gateway must be configured" 10  60
                Menu
          fi

         IP=${networkarray[0]}
         Netmask=${networkarray[1]}
         Gateway=${networkarray[2]}
         DNS1=${networkarray[3]}
         DNS2=${networkarray[4]}
         DNS3=${networkarray[5]}


        ip_check $IP
        IP_CheckContinue "IP"

        ip_check $Netmask
        IP_CheckContinue "Netmask"

        ip_check $Geteway
        IP_CheckContinue "Geteway"

        ip_check $DNS1
        IP_CheckContinue "DNS1"

        ip_check $DNS2
        IP_CheckContinue "DNS2"

        ip_check $DNS3
        IP_CheckContinue "DNS3"

        #再次确认
        dialog --title "Continue to confirm" --no-shadow --yesno "IP:$IP\nNetmask:$Netmask\nGateway:$Gateway\nDNS1:$DNS1\nDNS2:$DNS2\nDNS3:$DNS3" 10 60
        if [[ $? -eq 0 ]];then
               dialog --title "Yes/No" --no-shadow --yesno "Select Yes and wait for the service check.\nSelect No to return to the main menu" 10 60
                 if [[ $? -eq 0 ]];then
                     ModuleNetwork
                  else
                       Menu
                 fi

        else
                 NetworkModify
         fi
    else
           Menu

    fi


}

IPFlag=-1
function ip_check(){
   IPAddress=$1

   VALID_CHECK=$(echo ${IPAddress} |awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
   if [[ "$VALID_CHECK" == "yes" ]]; then
        IPFlag=1
    else
         IPFlag=-1
    fi
}


function activateServerinfo(){
        GetNetworkinfo
        dialog --title "ActivateServerinfo" --msgbox "http://$IP:8081/opsapi/" 10 60
        if [[ $? -eq 0 ]];then
             Serversinfo
      else
            Serversinfo
      fi
}

function activateServermge(){
     DISTROS=$(dialog --radiolist\
     "opsmanage" 10 60 2 "start" "opsmanage" off \
      "stop" "opsmanage" off 3>&1 1>&2 2>&3)
     exitstatus=$?
    if [ $exitstatus == 0 ]; then
        if [ $DISTROS == "start" ];then
            opsstart
        else
            opsstop

        fi
    else
        echo "You chose Cancel."
    fi
}


PROG=opsmange
PID=${PROG:+$PROG.pid}
function opsstart(){
    start_process() {
            # nohup ./${PROG} & echo $! >${PID}  2>&1
            nohup ./${PROG} >/dev/null 2>&1  & echo $! >${PID}  2>&1
            # if sleep 2 ; kill -0 `<${PID}` 2>/dev/null ;then
            #     echo  "Starting ${PROG}:"
            # else
            #     echo  "Starting ${PROG}:" ; exit 1
            # fi
        }
        cd /app/opsinit
        if [[ -f ${PID} ]] ;then
            if [[ -w ${PID} ]] ;then
                if [[ -L /proc/`<${PID}`/exe ]] 2>/dev/null ;then
                    echo "${PROG} Already Running"
                else
                    start_process
                fi
            else
                echo 'PID File: Permission denied'
            fi
        else
            start_process
        fi
}

function opsstop(){
     stop_process() {
            kill -15 `cat ${PID} 2>/dev/null` >/dev/null 2>&1
            # if sleep 2 ; kill -0 `cat ${PID} 2>/dev/null` 2>/dev/null ;then
            #     echo  "Stopping ${PROG}:" ;
            # else
            #     echo  "Stopping ${PROG}:"
                rm -f ${PID} >/dev/null 2>&1
            # fi
        }
        cd /app/opsinit
        if [[ -f ${PID} ]] ;then
            if [[ -w ${PID} ]] ;then
                if [[ -L /proc/`<${PID}`/exe ]] 2>/dev/null ;then
                    if [[ -w /proc/`<${PID}`/mem ]] ;then
                        stop_process
                    else
                        echo "Process: Permission denied"
                    fi
                else
                    echo 'Process: No such Process' ; rm -f ${PID}
                fi
            else
                echo 'PID File: Permission denied'
            fi
        else
            echo 'PID File: No such PID file'
        fi
}

Auth(){
    PASSWORD=$(dialog --title "AUTH"  --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
       if  [ $PASSWORD == "ytx_zhaokai" ];then
            exit
       fi
    else
        dialog  --title   "Warning"   --msgbox  "Password error" 10  60
    fi
}

function ModuleNetwork(){

         IP=${networkarray[0]}
         Netmask=${networkarray[1]}
         Geteway=${networkarray[2]}
         DNS1=${networkarray[3]}
         DNS2=${networkarray[4]}
         DNS3=${networkarray[5]}

          #网卡初始化
        sed -i s#IPADDR.*#IPADDR=$IP#g    $NETWORKFILE
        sed -i s#NETMASK.*#NETMASK=$Netmask#g   $NETWORKFILE
        sed -i s#GATEWAY.*#GATEWAY=$Geteway#g  $NETWORKFILE
        sed -i "s#^DNS1.*#DNS1=$DNS1#g" $NETWORKFILE
        sed -i "s#^DNS2.*#DNS2=$DNS2#g" $NETWORKFILE
        sed -i "s#^DNS3.*#DNS3=$DNS3#g" $NETWORKFILE


        systemctl restart network >/dev/null 2>&1


 
     for i in {1..100} ;do echo $i; sleep 0.1 ;done | dialog --title "initialization" --gauge "Please wait while initialization is underway"  10 50
     dialog --title "Message" --msgbox "Network  configuration successful." 10 60
}


CheckPid() {
    PID=$1

        stop_process() {
            kill -15 `cat ${PID} 2>/dev/null`
            if sleep 2 ; kill -0 `cat ${PID} 2>/dev/null` 2>/dev/null ;then
                echo  "Stopping ${PROG}:"
            else
                rm -f ${PID}
            fi
        }
        if [[ -f ${PID} ]] ;then
            if [[ -w ${PID} ]] ;then
                if [[ -L /proc/`<${PID}`/exe ]] 2>/dev/null ;then
                    if [[ -w /proc/`<${PID}`/mem ]] ;then
                        stop_process
                    else
                        echo "Process: Permission denied"
                    fi
                else
                    echo 'Process: No such Process' ; rm -f ${PID}
                fi
            else
                echo 'PID File: Permission denied' ; exit 1
            fi
        else
            echo 'PID File: No such PID file'
        fi
}
CheckLic(){
    licbin=/app/licenseserver/bin/
    pslicstart=licstart
    netstat -lnupt |egrep "0 0.0.0.0:7779" >/dev/null
    if [ $? -ne 0 ];then
        ps axu |grep $pslicstart | egrep -v "grep" >/dev/null
        if [ $? -ne 0 ] ;then
            cd /app/licenseserver/bin/
            ./licstart  >/dev/null &
        fi
    fi

}
CleanData(){
    cd /var/log && >./wtmp  >/dev/null
 }



main(){
        while [ 1 ];do
            trapper
            Menu
        done
}

main

posted @ 2019-08-16 10:31  运维面试辅导  阅读(282)  评论(0)    收藏  举报