#!/bin/bash

# ********************************************** ***************************** #
# File Name: 
# Description: 
# Input: 
# Output: 
# Author: 
# Created: 
# Function List：
# History 
# 1.Date: 
#   Author: 
#   Modification: 
# **************************************************************************** #
trap '' 20
trap "OS_KILL_CMD" 2


# --------------------------  variables  ------------------------------------- #
#变量定义
G_CMD_PARAM=""
G_SUB_PARAM=""
G_USER=""
G_USER_TYPE=""
# Replacing the command to delete binary file of realpath.
G_REALPATH_CMD="/usr/bin/readlink -f"
#开放的命令列表
G_ALLOWED_CMD_LIST_FILE="/usr/local/etc/os_debug_mode_cmd_list"
#允许访问目录的文件
G_ALLOWED_ACCESSED_FILE="/usr/local/etc/sftppermit.config"
#不允许访问目录的文件
G_DENIED_ACCESSED_FILE="/usr/local/etc/sftpdeny.config"
#下列命令只能在某些目录下执行
G_ALLOWED_SPECIAL_CMD=("ls" "rm" "mv" "cd" "cat" "mount" "du" "grep")
G_ALLOWED_PARAM_CMD=("showdiskinfo" "telnet" "ip" "df" "ifconfig" "showsystrace" "os_backup_info.sh" "os_change_bayconfig.sh" "os_write_bay_cfg.sh" "os_upgrade_hotpatch.sh" "sysforcepoweron" "os_change_tp.sh" "sshtoremoteExt" "getremotefile" "getremotedir" "showrootfsinfo" "showresetinfo" "rebootsys")
G_DISK_MOUNT=/dev/sda
G_MIN_MEM=430
G_EMERGENCY_CMD=("kill" "rm")
G_CURRENT_EXIST_CMD=("cat" "cd" "date" "disktool" "du" "free" "grep" "hwclock" "iostat" "kill" "ls"
            "lsscsi" "mount" "mv" "ps" "rm" "top" "umount" "ping"  "arp" "ip" "route" "netstat"
            "os_change_tp.sh")
G_NO_NEED_SUDO_CMD=("cd")
G_VERIFICATION_AND_DANGER_CMD=("cleardb" "logzonefreelogbylogid.sh" "modifydbtableinfo.sh")
G_ALL_REPLACE_PARAM_CMD=("om_show_omm_info.sh")
G_USER=$1
seg_ip=$2
G_ERR_ID=$3
G_ERR=$4


G_USER_TYPE=`cat /etc/passwd | awk -F: '{if($1 == var) print $4}' var=$G_USER`
L_MY_PID=$$

#检查用户是否合法，该模式只允许admin和对端通过心跳口使用
if [ "$G_USER" == "ibc_os_hs" -a "${seg_ip}" == "127.127.127" ];then
	/bin/bash
	exit
elif [ "$G_USER_TYPE" != "0" ];then
	echo "The minisystem mode is allowed for a super user only"
	exit
fi

#P12V-2045 modefied 2012/9/19 end
#包含头脚本
source /OSM/script/ocean_load_base.sh
# --------------------------  functions  ----------------------- ------------- #
# **************************************************************************** #
# Function Name: OS_PRINT_SYSTEM_INFO
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #

#2018/1/9
source /OSM/script/cli_verification_danger_funtion.sh
# **************************************************************************** #
# Function Name: CLI_VERIFICATION_AND_DANGER
# Description:白名单防呆处理，增加验证码输入与高危提示
# Parameter:
# Return: 0-防呆验证成功/不需要防呆; 1-防呆验证不成功
# **************************************************************************** #

OS_PRINT_SYSTEM_INFO()
{
        if [ -f /OSM/conf/versions.conf ];then
        L_PRODUCT_VERSION=`cat /OSM/conf/versions.conf | grep "^SpcVersion=" -m 1 2>/dev/null`
        L_PRODUCT_VERSION=${L_PRODUCT_VERSION:11:11}
        L_SYSTEM_VERSION=`cat /OSM/conf/versions.conf | grep "^Version=" | sed -n '2p' 2>/dev/null`
        L_SYSTEM_VERSION=${L_SYSTEM_VERSION:8:1}.${L_SYSTEM_VERSION:9:2}.${L_SYSTEM_VERSION:11:2}.${L_SYSTEM_VERSION:13:3}
        L_PATCH_VERSION=`cat /OSM/conf/versions.conf | grep "^SpcVersion=" -m 1 2>/dev/null`
        L_PATCH_VERSION=${L_PATCH_VERSION:22:6}
        L_COMPILE_TIME=`cat /OSM/conf/versions.conf | grep "^Time=" -m 1 2>/dev/null`
        L_COMPILE_TIME=${L_COMPILE_TIME:5:19}
        if [ -n "${L_PRODUCT_VERSION}" -a -n "${L_SYSTEM_VERSION}" -a -n "${L_COMPILE_TIME}" ];then
            echo "-----------------System Information-----------------"
            echo "|  Product Version     |   ${L_PRODUCT_VERSION}             |"
            echo "|  System  Version     |   ${L_SYSTEM_VERSION}             |"
            if [ -n "${L_PATCH_VERSION}" ];then
	        echo "|  Patch   Version     |   ${L_PATCH_VERSION}                  |"
	    else
	        echo "|  Patch   Version     |   "      "                       |"
	    fi
            echo "|  Release Time        |   ${L_COMPILE_TIME}     |"
            echo "----------------------------------------------------"
        fi
    fi
}

# **************************************************************************** #
# Function Name: OS_KILL_CMD
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_KILL_CMD()
{
    return 0
}

# **************************************************************************** #
# Function Name: OS_CHECK_CURRENT_EXIST_CMD
# Description:
# Parameter: cmd needed to check
# Return: exist 1, otherwise 0
# **************************************************************************** #
OS_CHECK_CURRENT_EXIST_CMD()
{
    local CMD_NEED_CHECK=$1

    for cmd in ${G_CURRENT_EXIST_CMD[@]}
    do
        if [ "${cmd}" == "${CMD_NEED_CHECK}" ];then
            return 1
        fi
    done

    return 0
}

#P12V-1984 modified 2012/9/10 begin
# **************************************************************************** #
# Function Name: OS_PRINT_HELP_INFO
# Description:
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_PRINT_HELP_INFO()
{
    local L_LINUX_CMDS=
    echo "Supported commands in minisystem mode:"            
    echo "exit          : quit"         
    echo "help          : show usage"           
    echo "rebootsys [opt]: reboot system"            
    echo "setupsystem   : format and re-install system"
    echo "showversion   : show versions"            
    echo "showbootmode  : show boot mode"
    echo "showdiskinfo  : show disk info"
    echo "upgradesystem : upgrade system software"
    echo "showsysstatus : show system status"
    echo "showsystrace [FlowId] : show system trace"
    echo "exportmemdb   : export memory db"
    echo "cleardb       : clear db in all mediums begin"
    echo "resumebootmode: resume boot mode from rescue to normal"
    echo "sshtoremote : ssh to peer via bond0 with ${G_USER}"
    echo "sshtoremoteExt [SlotId] : ssh to the controller specified by SlotId with ${G_USER}"
    echo "iprulelist : list ip rules"
    #SR-0000288844-支持小系统日志收集 added on 2013-10-18 begin
    echo "os_backup_info.sh [-f xxx.tar.bz2] [-a] [-F] | [-p] : collect logs"
    echo "                  -f: specify the saved log package name"
    echo "                  -a: backup all log files"
    echo "                  -F: backup log files without size limitation"
    echo "                  -p: check whether memory is sufficient or not"
    #SR-0000288844-支持小系统日志收集 added on 2013-10-18 end
    #AR-0000762469-支持6U4C的2扩4方案 added on 2014-05-22 begin
    echo "os_change_bayconfig.sh [delete] | [change] | [set] | [sync]: change bay config for BMC"
    #AR-0000762469-支持6U4C的2扩4方案 added on 2014-05-22 end
    echo "sysforcepoweron [switch flag] : set force power on switch flag [0:open,1:close]"
    echo "os_write_bay_cfg.sh [-i BayID] [-c ControllerTotal] [-t BayType] [-n] [-l] [-s] [-d]|[-a]"
    echo "os_upgrade_hotpatch.sh [-m size] [-w install|roll|delete] [-p package name [mini] ] [-g package name] [-c] [-r] [-k] [-d] [-s [1|2] ] [-v all|local|peertolocal] [-h] : upgrade hotpatch interface"
    echo "os_change_tp.sh { -a | -d } -f func -t type -i alive -p param : change tracepoint config file"
    echo "changeibckey : replace ibc_os_hs's key files to the home directory of ibc_os_hs"
    echo "getremotefile ip src_file dst_dir : transfer file between controllers"
    echo "getremotedir ip src_dir dst_dir : transfer dir between controllers"
    echo "telnet IPv4|IPv6 port(1~65535) : The telnet command is used to check whether the client port is open."
    echo "df -h : obtain the overall usage of disks"
    echo "showrootfsinfo : The showrootfsinfo command is used to query files in the cache and print the files into the messages file."
    echo "showresetinfo : The showresetinfo command is used to query the reset cause and time recorded by nvram."
    echo "showbayconfig : The showbayconfig command is used to show the bay information in memory and disks, including the enclosure ID, number of controllers and so on."
    echo "enableiocfw : The enableiocfw command is used to update firmware of ioc smartio cards of this controller."
    echo "disableiocfw : The disableiocfw command is used to resume firmware of ioc smartio cards of this controller."
	echo "enablesesfw : The enablesesfw command is used to update firmware of manage_chip of this controller."
    echo "disablesesfw : The disablesesfw command is used to resume firmware of manage_chip of this controller."	
    
    if [ -f ${G_ALLOWED_CMD_LIST_FILE} ]; then
        count=0
        product_name=`cat /OSM/conf/product.ini | grep "^ProductName=" -m 1 2>/dev/null`
        while read line
        do
            if [ -z "$line" ]; then
                continue
            fi
            if [ "restore_sysenclosure_info.sh" == "$line" ]; then  
                if [ "ProductName=2600 V3" != "$product_name" ];then
		    if [ "ProductName=2600F V3" != "$product_name" ];then
                    continue;
                    fi
                fi
            fi
            OS_CHECK_CURRENT_EXIST_CMD ${line}
            ret=$?
            if [ 1 -eq "$ret" ]; then
                continue

            else
                if [ -n "`echo ${line} | grep "\.sh$"`" ];then
                    help_info=`${line} -h`
                    [ -n "$help_info" ] && echo $help_info
                else
                    ((count++))
                    # 每六个命令换行一次，因此插入逗号，方便匹配替换
                    if [ $count -eq 6 ]; then
                        count=0;
                        L_LINUX_CMDS=${L_LINUX_CMDS}${line}","
                    else
                        L_LINUX_CMDS=${L_LINUX_CMDS}${line}" "
                    fi
                fi
            fi
        done<${G_ALLOWED_CMD_LIST_FILE}
    fi

    echo "Other command supported:"
    echo "cat cd date disktool"
    echo "du free grep hwclock ifconfig"
    echo "iostat kill ls lsscsi mount ping"
    echo "mv ps rm top umount"
    echo "arp ip route netstat"
    [ -n "$L_LINUX_CMDS" ] && echo ${L_LINUX_CMDS%?} | sed "s/,/\n/g"
    return 0
}
#P12V-1984 modified 2012/9/10 end

# **************************************************************************** #
# Function Name: OS_PERMITDIR_CHECK
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_PERMITDIR_CHECK()
{
    local L_CHECK_DIR=$1
    local L_RESULT="0"
    L_CHECK_DIR=`${G_REALPATH_CMD} ${L_CHECK_DIR}`
    
    while read line 
    do             
        L_RESULT=`expr match ${L_CHECK_DIR} ${line} 2>/dev/null`
        if [ "${L_RESULT}" != "0" ];then 
            return 1 
        fi 
    done<${G_DENIED_ACCESSED_FILE}
    
    while read line
    do            
        L_RESULT=`expr match ${L_CHECK_DIR} ${line} 2>/dev/null`
        if [ "${L_RESULT}" != "0" ];then
            return 0
        fi
    done<${G_ALLOWED_ACCESSED_FILE}

    return 1
}

# **************************************************************************** #
# Function Name: OS_CHECK_OP_VALID
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_CHECK_OP_VALID()
{
    local L_CMD_OP=$1
    local L_CMD_DATA=$2
    local L_RET=0

    for cmd in ${G_ALLOWED_SPECIAL_CMD[@]}
    do
        if [ "${cmd}" == "${L_CMD_OP}" ];then
            for dir in ${L_CMD_DATA} 
            do
               if [ ${dir:0:1} = "-" ]; then
                    continue;
               fi


                if [ "${cmd}" == "mount" ];then
                    L_DEV=`echo ${dir} | grep "^/dev"`
                    #包含/dev字符串，允许mount
                    if [ -n "${L_DEV}" ];then
                        continue
                    fi
                fi
                
                OS_PERMITDIR_CHECK "${dir}"
                L_RET=$?
                if [ "$L_RET" != "0" ];then
                    return 1
                fi
            done
        fi
    done

    return 0
}
# **************************************************************************** #
# Function Name: OS_CHECK_RPLACE_PARAMCMD_VALID
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_CHECK_RPLACE_PARAMCMD_VALID()
{
    local L_CMD=`echo $G_CMD_PARAM | awk '{print $1}'`

    for cmd in ${G_ALL_REPLACE_PARAM_CMD[@]}
    do
        if [ "${cmd}" == "${L_CMD}" ];then
            return 0
        fi
    done
    [ -n "$(echo "$G_CMD_PARAM" | grep '\\')" ] && { echo "This mode not support command with execution character!"; return 1; }
	
    return 0
}
# **************************************************************************** #
# Function Name: OS_CHECK_PARAMCMD_VALID
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_CHECK_PARAMCMD_VALID()
{
    local L_CMD=`echo $G_CMD_PARAM | awk '{print $1}'`

    for cmd in ${G_ALLOWED_PARAM_CMD[@]}
    do
        if [ "${cmd}" == "${L_CMD}" ];then
            G_SUB_PARAM=$L_CMD
            break
        fi
    done

    return 0
}
# **************************************************************************** #
# Function Name: OS_CHECK_CMD_VALID
# Description: 
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_CHECK_CMD_VALID()
{
    local L_PWD=`pwd`
    #local L_CMD_TMP=`echo ${G_CMD_PARAM} | cut -d" " -f 1`
    #local L_CMD_EXECUTE=${L_CMD_TMP##*/}
    local L_CMD_EXECUTE=`echo ${G_CMD_PARAM} | cut -d" " -f 1`
    local L_CMD_LENGTH=${#L_CMD_EXECUTE}
    local L_CMD_DATA=${G_CMD_PARAM:$L_CMD_LENGTH}
    L_CMD_DATA=${L_CMD_DATA%"|"*}
    #P11G-5413 modified 2012/9/10 begin

    for line in ${G_CURRENT_EXIST_CMD[@]}
    do
        if [ "${line}" == "${L_CMD_EXECUTE}" ];then
            if [ -z "${L_CMD_DATA}" ];then
                return 0
            fi
            #对于特殊命令，需要限制操作范围，比如，文件路径等
            OS_CHECK_OP_VALID "${L_CMD_EXECUTE}" "${L_CMD_DATA}"
            if [ "$?" == "0" ];then
                return 0
            else
                echo "${G_CMD_PARAM} : Permission denied."
                return 1
            fi
        fi
    done

    if [ -f ${G_ALLOWED_CMD_LIST_FILE} ]; then
        while read line
        do
            if [ "${line}" == "${L_CMD_EXECUTE}" ];then
                if [ -z "${L_CMD_DATA}" ];then
                    return 0
                fi
                #对于特殊命令，需要限制操作范围，比如，文件路径等
                OS_CHECK_OP_VALID "${L_CMD_EXECUTE}" "${L_CMD_DATA}"
                if [ "$?" == "0" ];then
                    return 0
                else
                    echo "${G_CMD_PARAM} : Permission denied."
                    return 1
                fi
            fi
        done<${G_ALLOWED_CMD_LIST_FILE}
    fi

    #P11G-5512 modified 2012/10/8 begin
    echo "Error : Command '${G_CMD_PARAM}' does not exist or has an incorrect format."
    echo "Try 'help' for more information."
    #P11G-5512 modified 2012/10/8 end
    
    return 1
}

# **************************************************************************** #
# Function Name: OS_CHECK_NO_NEED_SUDO_CMD
# Description: if eth checked cmd is listed in G_NO_NEED_SUDO_CMD, return 1
# Parameter: cmd needed to check
# Return: exist 1, otherwise 0
# **************************************************************************** #
OS_CHECK_NO_NEED_SUDO_CMD()
{
    local CMD_NO_NEED_SUDO=$1

    for cmd in ${G_NO_NEED_SUDO_CMD[@]}
    do
        if [ "${cmd}" == "${CMD_NO_NEED_SUDO}" ];then
            return 1
        fi
    done

    return 0
}

    #P11G-5413 modified 2012/9/10 end
    
# **************************************************************************** #
# Function Name: OS_EXECUTE_DEBUG_CMD
# Description: 
#因为SysV的规定，当字符串中包含管道时，无法执行
#目前只支持最多10个管道的命令，暂时实现方法，后续有
#更好的方法，可以优化
# Parameter: 
# Return: success 0, otherwise 1
# **************************************************************************** #
OS_EXECUTE_DEBUG_CMD()
{
    declare -a split_array=()
    local split_argv1=${G_CMD_PARAM}
    local split_argv_tmp=${G_CMD_PARAM}
    if echo ${split_argv_tmp} | grep -w 'vi' >/dev/null 2>/dev/null
    then
    split_argv1=`echo ${split_argv_tmp} | sed "s#vi#vi -Z#g"`
    fi
    local split_argv2="|"
    if [ "$split_argv2" == "" ];then 
        return 1 
    fi 

    split_len=0 

    break_len=${#split_argv2} 

    #判断是否包含分隔符
    index=`expr index "$split_argv1" "$split_argv2"` 

    while [[ "$index" -gt "0" ]] 
    do 
        #获得分割好的字符串 
        split_array[$split_len]=${split_argv1:0:$index-1} 
        split_len=$split_len+1

        #记录截断的字符串 
        split_argv1=${split_argv1:$index-1+break_len} 
        index=`expr index "$split_argv1" "$split_argv2"` 
    done 

    #把最后一个保存 到数组中
    split_array[$split_len]=$split_argv1
    
    alen=${#split_array[*]}

    OS_CHECK_NO_NEED_SUDO_CMD ${split_array[0]}
    if [ 1 -eq "$?" ]; then
        case $alen in
            1)
                ${split_array[0]}
                ;;
            2)
                ${split_array[0]}|${split_array[1]}
                ;;
            3)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}
                ;;
            4)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}
                ;;
            5)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}
                ;;
            6)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}
                ;;
            7)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}
                ;;
            8)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}
                ;;
            9)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[7]}         
                ;;
            10)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[8]}|${split_array[9]}
                ;;
            11)
                ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[8]}|${split_array[9]} |${split_array[10]}           
                ;;
            *)
                ;;
        esac
    else
        case $alen in
            1)
                sudo ${split_array[0]}
                ;;
            2)
                sudo ${split_array[0]}|${split_array[1]}
                ;;
            3)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}
                ;;
            4)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}
                ;;
            5)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}
                ;;
            6)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}
                ;;
            7)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}
                ;;
            8)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}
                ;;
            9)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[7]}         
                ;;
            10)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[8]}|${split_array[9]}
                ;;
            11)
                sudo ${split_array[0]}|${split_array[1]}|${split_array[2]}|${split_array[3]}|${split_array[4]}|${split_array[5]}|${split_array[6]}|${split_array[7]}|${split_array[8]}|${split_array[9]} |${split_array[10]}           
                ;;
            *)
                ;;
        esac
    fi

    if [ "$?" != 0 ];then
        return 1
    fi

    return 0
}



# **************************************************************************** #
# Function Name: OS_ALARM_CMD_EMERGENCY
# Description:检查待执行命令是否为高危命令
# Parameter:
# Return: 1-是; 0-否
# **************************************************************************** #
OS_ALARM_CMD_EMERGENCY()
{
    local L_CMD_EG=`echo ${G_SUB_PARAM} | cut -d" " -f 1`
    for cmd in ${G_EMERGENCY_CMD[@]}
    do
        if [ "${cmd}" == "${L_CMD_EG}" ]; then
            return 1
        fi
    done
    
    return 0
}

# **************************************************************************** #
# Function Name: OS_CHECK_HELP_CMD
# Description:检查待执行命令是否为查看帮助的命令
# Parameter:
# Return: 1-是; 0-否
# **************************************************************************** #
OS_CHECK_HELP_CMD()
{
    local L_CMD_SEG=`echo ${G_SUB_PARAM} | cut -d" " -f 2`
    if [ "${L_CMD_SEG}" == "-h" -o "${L_CMD_SEG}" == "--help" ]; then
        return 1
    fi
     return 0
}

# --------------------------  main  ------------------------------------------ #
G_WHERE=`who -m | awk '{print $2$6}'`

OS_echo "[minisystem]$G_USER login minisystem(pid=$L_MY_PID) from $G_WHERE."

G_SYS_RECORD_STATUS=/OSM/log/runlog/sys_power_on_record
G_PRINTINFO=0
#打印系统信息
if [ -f $G_SYS_RECORD_STATUS ];then
   if [ -n "$G_ERR" -o -n "$G_ERR_ID" ];then
          G_PRINTINFO=1
          OS_PRINT_SYSTEM_INFO
   fi
else
   G_PRINTINFO=1
   OS_PRINT_SYSTEM_INFO
fi

#os_rescue模式特殊处理
oSRescue=`cat /proc/cmdline | grep os_rescue`
if [ -n "$oSRescue" ];then
  if [ "${G_PRINTINFO}" == 0 ];then
    OS_PRINT_SYSTEM_INFO
  fi
fi


#打印错误信息
if [ -f /OSM/script/ErrCodeFunc.sh ];then
        source /OSM/script/ErrCodeFunc.sh
        if [ -f $G_SYS_RECORD_STATUS -a -z "$G_ERR" ];then
          if [ -z "$G_ERR_ID" -a -z "$oSRescue" ];then
              sh /ISM/cli/start.sh -u admin
              exit
          fi
        fi

        if [ -n "$G_ERR_ID" -a -n "$G_ERR" ];then
                showErrorInfo "$G_ERR_ID" "$G_ERR"
        elif [ -n "$G_ERR_ID" ];then
            showErrorInfo "$G_ERR_ID"
        fi
else
        OS_echo "[minisystem]/OSM/script/ErrCodeFunc.sh is not exist, can't show error_info."
fi



PATH=$PATH:/sbin:/usr/local/sbin:/usr/sbin:/usr/local/OSM/script:/OSM/bin:/OSM/bin/script:/OSM/script
export PATH
OS_echo "[minisystem]admin login minisystem(pid=$L_MY_PID)."

#切换到用户主目录
cd /home/permitdir

#主循环
L_PROMPT="$(hostname): minisystem> "
while true
do       
    L_FD=`ls -l /proc/$L_MY_PID/fd/0 2>/dev/null | awk '{print $9 $10 $11 $12}' | grep deleted`
    if [ "${L_FD}x" != "x" ];then
        OS_echo "[minisystem]stdin-fd deleted already, so auto-exit minisystem(pid=$L_MY_PID)."
        exit 1
    fi
    G_CMD_PARAM=""
    read -p "$L_PROMPT" G_CMD_PARAM
    [ $? -ne 0 ] && echo " " && exit 1

    if [ -z "$G_CMD_PARAM" ];then
        continue
    fi

    if [ -n "$G_CMD_PARAM" ];then
        OS_RECORD_HISTORY_CMD "$G_CMD_PARAM" "ospcli_history"
    fi
    [ -n "$(echo "$G_CMD_PARAM" | grep '|')" ] && { echo "This mode not support command with pipe!"; continue; }
    [ -n "$(echo "$G_CMD_PARAM" | grep ';')" ] && { echo "This mode not support command with semicolon!"; continue; }
    [ -n "$(echo "$G_CMD_PARAM" | grep '&')" ] && { echo "This mode not support command with and!"; continue; }
    [ -n "$(echo "$G_CMD_PARAM" | grep '\$')" ] && { echo "This mode not support command with dollar!"; continue; }
    [ -n "$(echo "$G_CMD_PARAM" | grep '`')" ] && { echo "This mode not support command with execution character!"; continue; }
    [ -n "$(echo "$G_CMD_PARAM" | grep '>')" ] && { echo "This mode not support command with execution character!"; continue; }
    OS_CHECK_RPLACE_PARAMCMD_VALID
    if [ $? != 0 ];then
        continue
    fi 
	
    G_SUB_PARAM=$G_CMD_PARAM
    OS_CHECK_PARAMCMD_VALID
    case ${G_SUB_PARAM} in
           "exit")
                echo "Are you sure to exit?(y/n)"
                read -p "" L_INPUT
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ];then
                    OS_echo "[minisystem]admin logout minisystem(pid=$L_MY_PID)."
                    exit
                fi
                ;;
            "help")
                OS_PRINT_HELP_INFO
                ;;
            "rebootsys")
			
                echo "Are you sure to restart this controller?(y/n)"
                read -p "" L_INPUT
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ];then
                    L_INSMOD_OSPSYS=`lsmod |awk '{print $1}' | grep 'sysctrl'`
                    if [ -z "$L_INSMOD_OSPSYS" ] ; then
                    	sudo reboot -f
                    	continue
                    fi
                    					
                    L_FORCE_FALG=`echo "$G_CMD_PARAM" | awk '{print $2}'`   
                    if [ -z "$L_FORCE_FALG" ];then   
                    	echo 'reboot controller'
                    	echo -e "sys ministopfd\nexit\n" | diagnose --auto
                    else
                    	if [ "force" == "$L_FORCE_FALG" ];then   
                    	   echo "reboot controller operate($L_FORCE_FALG):"   
                    	   sudo reboot -f
                    	   continue
                    	else
                    	    echo "Invalid parameter, if you want to force reboot it, please execute 'rebootsys force'."   
                    	    continue
                    	fi
                    fi  

                fi 
                ;;
            "setupsystem")    
                L_PWD=`pwd`
                if [ ! -f /home/permitdir/OceanStor_*.tgz ];then                    
                    sudo /OSM/script/os_check_sys_free_mem.sh "${G_MIN_MEM}"
                    if [ 0 != "$?" ];then
                        echo "Re-install system failed,current system memory is lower to ${G_MIN_MEM}M."
                        continue
                    fi 
                fi
                echo "Make sure you have uploaded package to /home/permitdir/"          
                read -p "Input package name:" L_PKG_NAME            
                if [ -n "$L_PKG_NAME" ];then                
                    L_TYPE=$(OS_GET_HW_TYPE)
                    sudo /OSM/script/os_setup_disk.sh -p /home/permitdir/$L_PKG_NAME -d /dev/sda -t $L_TYPE
                    if [ "$?" == "0" ];then                 
                       echo "Re-install system success, you must restart the system for the change to take effect."                
                    else                    
                       echo "Re-install system failed, reboot system is dangerous, please setupsystem again."              
                    fi              
                else                
                    echo "package name can not be empty"            
                fi
                cd ${L_PWD} &>/dev/null
                ;;
    #问题单号:P12N-5156   时间:20121109 begin      
            "showversion")          
                if [ -f /OSM/conf/versions.conf ];then              
                    echo "Product Version  |  ${L_PRODUCT_VERSION}"             
                    echo "System  Version  |  ${L_SYSTEM_VERSION}"              
                    echo "Release Time     |  ${L_COMPILE_TIME}"            
                else                
                    echo "version file is not exist"            
                fi          
                ;;      
            "showbootmode") 
                L_PWD=`pwd`
                if [ -L /startup_disk/image/boot ];then             
                    cd /startup_disk/image &>/dev/null              
                    ls -l boot 2>/dev/null             
                    cd / &>/dev/null            
                else                
                    echo "boot link is not exist"           
                fi
                cd ${L_PWD} &>/dev/null
                ;;   
            "showrootfsinfo") 

                eval sudo /OSM/script/show_rootfs_files_info.sh ${G_CMD_PARAM#showrootfsinfo}
                ;; 
            "showresetinfo") 
                sudo /OSM/script/upgrade.sh -s
                ;;
            "showbayconfig") 
                sudo /OSM/script/os_show_bay_cfg.sh
               ;;                
            "resumebootmode") 
                L_PWD=`pwd`
                sudo /OSM/script/os_resume_boot_mode.sh
                if [ "$?" != "0" ];then
                    cd ${L_PWD} &>/dev/null
                    continue
                fi

                cd ${L_PWD} &>/dev/null
                ;;
            "upgradesystem")
                L_PWD=`pwd`
                sudo /OSM/script/os_upgrade_system.sh
                if [ "$?" != "0" ];then
                    cd ${L_PWD} &>/dev/null
                    continue
                fi

                cd ${L_PWD} &>/dev/null
                ;;
            "showsysstatus")
                L_INSMOD_OSPSYS=`lsmod |awk '{print $1}' | grep 'sysctrl'`
                if [ -z "$L_INSMOD_OSPSYS" ] ; then
                    echo 'The command do not execute.'
                    continue
                fi
                              
                echo "Show system status"
                echo -e "sys showcls\nexit\n" | diagnose --auto
                ;;
        "showsystrace")   
                L_INSMOD_OSPSYS=`lsmod |awk '{print $1}' | grep 'sysctrl'`   
                if [ -z "$L_INSMOD_OSPSYS" ] ; then   
                    echo 'The command do not execute.'   
                    continue   
                fi   
                     
               L_FLOW_ID=`echo "$G_CMD_PARAM" | awk '{print $2}'`   
               if [ -z "$L_FLOW_ID" ];then   
                   echo 'Show flow all:'
                   echo -e "sys showtrace\nexit\n" | diagnose --auto
               else
                   echo "Show system flow($L_FLOW_ID):"   
                   echo -e "sys showtrace $L_FLOW_ID\nexit\n" | diagnose --auto  
               fi   
               ;;
            "exportmemdb")
                L_INSMOD_EXPORTMEMDB=`lsmod |awk '{print $1}' | grep 'dbapp'`
                L_INSMOD_EXPORTMEMDBB=`lsmod |awk '{print $1}' | grep 'dbmanage'`
                if [ -z "$L_INSMOD_EXPORTMEMDB" -o -z "$L_INSMOD_EXPORTMEMDBB" ] ; then
                    echo 'The command do not execute.'
                    continue
                fi
                
                echo "Export db from memory begin......"
                echo -e "db exportdb\nexit\n" | diagnose --auto
                ;;
            "cleardb")
                L_INSMOD_DB=`lsmod |awk '{print $1}' | grep 'dbapp'`
                if [ -z "$L_INSMOD_DB" ] ; then
                    echo 'The command do not execute.'
                    continue
                fi

                #白名单防呆处理，增加验证码输入与高危提示
                CLI_VERIFICATION_AND_DANGER
                if [ $? -ne 1 ]; then
                  cd ${L_PWD} &>/dev/null
                  continue
                fi
                
                echo "DANGER: You are going to clear the configuration data of the storage system and the previous configuration data will be lost."
                echo "Suggestion: Before you perform this operation, export the configuration data ."
                echo "Have you read danger alert message carefully?(y/n)"
                read -p "" L_INPUT
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ];then
                    echo "Clear db in all mediums begin......"
                    echo -e "db clearall\nexit\n" | diagnose --auto
                fi
        
        #P11G-5436  modified 2012/9/10 end 
                ;;
            "sshtoremote")
                echo "Ssh to peer via bond0 with ${G_USER}......"
                L_REMOTE_IBC_IP=`OS_GetRemoteIBCIP`
                L_PORT=`sudo /OSM/script/os_get_sshd_port.sh`
                ssh -p "${L_PORT}" ${G_USER}@${L_REMOTE_IBC_IP}
                ;;
            "sshtoremoteExt")
                L_RET=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 2) print 1; else print 0}'`
                if [ "$L_RET" -ne "0" ];then
                    echo "Input parameter number error!"
                    echo "Try 'help' for more information."
                    continue
                fi
                L_SLOT_ID=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                L_OTHER_IBC_IP=`OS_GetIPBySlotId $L_SLOT_ID`
                if [ -z "$L_OTHER_IBC_IP" ]; then
                    echo "SlotId is invalid."
                    continue
                fi

                echo "Ssh to controller(SlotId=${L_SLOT_ID}) with ${G_USER}......"
                L_PORT=`sudo /OSM/script/os_get_sshd_port.sh`
                ssh -p "${L_PORT}" ${G_USER}@${L_OTHER_IBC_IP}
                ;;
            "getremotefile")
                L_RET=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 4) print 1; else print 0}'`
                if [ "$L_RET" -ne "0" ];then
                    echo "Input parameter number error!"
                    echo "Try 'help' for more information."
                    continue
                fi
                L_IP=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                L_SRC_FILE=`echo "$G_CMD_PARAM" | awk '{print $3}'`
                L_DST_FILE=`echo "$G_CMD_PARAM" | awk '{print $4}'`             
                if [ ! -d "${L_DST_FILE}" ];then
                    echo "${L_DST_FILE} is not exist." 
                    continue
                fi
                OS_PERMITDIR_CHECK "${L_DST_FILE}"            
                if [ $? != 0 ];then
                    echo "Permission denied."                   
                    continue
                fi                
                L_TMP=`echo $L_IP | grep "127.127.127"`    
                if [ -n "$L_TMP" ];then
                OS_PERMITDIR_CHECK "${L_SRC_FILE}"
                if [ $? != 0 ];then
                    echo "Permission denied."                
                    continue
                fi
                    sudo /OSM/script/os_get_file.sh "$L_IP" "$L_SRC_FILE" "$L_DST_FILE"
                else
                    L_CFG_ETH_SEG=`echo ${L_IP} | awk -F. '{print $1"."$2"."$3}'`
                    L_FTP_PROTOCOL="sftp://"
                    L_PORT=`sudo /OSM/script/os_get_sshd_port.sh`
                    L_LFTP_RET=/tmp/lftp_return_$$
                    sudo lftp -u ${G_USER} ${L_FTP_PROTOCOL}${L_IP}:"${L_PORT}" -e "set xfer:disk-full-fatal yes;set net:reconnect-interval-base 3;set net:max-retries 2;set net:timeout 30s;set xfer:clobber on;debug 4 -o ${L_LFTP_RET};get ${L_SRC_FILE} -o ${L_DST_FILE}; bye" &>/dev/null
                fi
                if [ $? != 0 ];then
                    echo "getremotefile failed."   
                    [ -f "${L_LFTP_RET}" ] && sudo cat "${L_LFTP_RET}"
                    [ -f "${L_LFTP_RET}" ] && sudo rm -f "${L_LFTP_RET}"
                    continue
                fi 
                echo "[getremotefile] execute successfully" 
                ;;            
            "getremotedir")
                L_RET=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 4) print 1; else print 0}'`
                if [ "$L_RET" -ne "0" ];then
                    echo "Input parameter number error!"
                    echo "Try 'help' for more information."
                    continue
                fi
                L_IP=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                L_SRC_DIR=`echo "$G_CMD_PARAM" | awk '{print $3}'`
                L_DST_DIR=`echo "$G_CMD_PARAM" | awk '{print $4}'`              
            
                OS_PERMITDIR_CHECK "${L_DST_DIR}"            
                if [ $? != 0 ];then
                    echo "Permission denied."                   
                    continue
                fi   
                L_TMP=`echo $L_IP | grep "127.127.127"`    
                if [ -n "$L_TMP" ];then
                    OS_PERMITDIR_CHECK "${L_SRC_DIR}"
                    if [ $? != 0 ];then
                        echo "Permission denied."                
                        continue
                    fi
                    sudo /OSM/script/os_get_dir.sh "$L_IP" "$L_SRC_DIR" "$L_DST_DIR" 
                else
                    L_CFG_ETH_SEG=`echo ${L_IP} | awk -F. '{print $1"."$2"."$3}'`
                    L_FTP_PROTOCOL="sftp://"
                    L_PORT=`sudo /OSM/script/os_get_sshd_port.sh`
                    G_SRC_DIR_NAME=${L_SRC_DIR##*/}
                    L_LFTP_RET=/tmp/lftp_return_$$
                    sudo lftp -u ${G_USER} ${L_FTP_PROTOCOL}${L_IP}:"${L_PORT}" -e "set xfer:disk-full-fatal yes;set net:reconnect-interval-base 3;set net:max-retries 2;set net:timeout 30s;set xfer:clobber on;debug 4 -o ${L_LFTP_RET};mirror ${L_SRC_DIR} ${L_DST_DIR}/${G_SRC_DIR_NAME}; bye" &>/dev/null
                fi
                if [ $? != 0 ];then
                    echo "getremotedir failed."  
                    [ -f "${L_LFTP_RET}" ] && sudo cat "${L_LFTP_RET}"
                    [ -f "${L_LFTP_RET}" ] && sudo rm -f "${L_LFTP_RET}"	
                    continue
                fi 
                echo "[getremotedir] execute successfully"
                ;;				
            "showdiskinfo")
                #检查sd模块是否已经加载
                L_INSMOD_BDMSD=`lsmod | grep 'sd ' | awk '{print $1}'`
                if [ -z "$L_INSMOD_BDMSD" ];then
                    echo 'The command do not execute.'
                    continue
                fi
                #需要执行的命令
                L_EXEC_SHOW_DEV="sddebug showdev"
                echo -e "${L_EXEC_SHOW_DEV}\nexit\n" | diagnose --auto
                ;;
            "iprulelist")
                L_RULE_LIST=/tmp/ip_rule_tmp
                ip rule list > $L_RULE_LIST
                if [ $? -eq 0 ];then
                    echo "==============================="
                    cat $L_RULE_LIST
                    echo "==============================="
                else
                    echo "Execute failed!"
                fi   
                ;;
			"ip")
				if [ -n "`echo "$G_CMD_PARAM" | grep -w "exec"`" ];then
					echo "command \"ip * exec *\" is forbided!"
				elif [ -n "`echo "$G_CMD_PARAM" | grep -w "bash"`" ];then
					echo "command \"ip * bash *\" is forbided!"
				else 
					L_TMP_RES=`echo "$G_CMD_PARAM" |grep -w "ip"`
					if [ -z "`echo $L_TMP_RES |grep -w "netns"`" -a -z "`echo $L_TMP_RES |grep -w "netn"`" -a -z "`echo $L_TMP_RES |grep -w "net"`" ];then
						sudo $G_CMD_PARAM
					else
						echo "command \"ip net* *\" is not supported!"
					fi	
				fi			
				;;
			#问题单DTS2016081606300
			"ifconfig")
				if [ -z "`echo  ${G_CMD_PARAM} | grep -w "tunnel"`" ];then
					sudo $G_CMD_PARAM
				else
					echo "Command \"ifconfig * tunnel *\" is not supported!"
				fi
				;;
			
            #SR-0000288844-支持小系统日志收集 added on 2013-10-18 begin
            "os_backup_info.sh")
                # if the inputted the parameter -f, the set f_flag=1
                f_flag=0
                # f_count means how many paramers after the parameter -f
                f_count=0
                L_PARAM1=" -d /OSM/coffer_data/omm/export_import"
                # the parameter para_check=0 means the inputted parameters are correct, not zero means wrong parameters
                para_check=0
                L_PARAM=${G_CMD_PARAM#os_backup_info.sh}
                if [ -n "${L_PARAM}" ];then
                    for param in ${L_PARAM}
                    do
                        if [ "${param}" == "-f" ];then
                            f_flag=1
                        elif [ "${f_count}" -eq 1 ];then
                            # after the parameter -f, the parameter must be the filename and its extension must be .tar.bz2
                            if [ "$(echo $param | awk -F'.' '{print $(NF-1)"."$NF}')" != "tar.bz2" ];then
                                echo "Parameter is not correct, the filename extension must be .tar.bz2."
                                para_check=1
                                break
                            fi
                        elif [ "${param}" != "-a" ] && [ "${param}" != "-F" ] && [ "${param}" != "-p" ];then
                            echo "Parameter is not correct, only the parameter -f, -a, -F and -p are supported."
                            para_check=1
                            break
                        fi
                        # the f_count add one to indicate how many paramers after the parameter -f
                        if [ "${f_flag}" -eq 1 ];then
                            f_count=$((f_count+1))
                        fi
                    done
                fi
				
                # after the parameter -f, must set the filename
                if [ "${f_flag}" -eq 1 ] && [ "${f_count}" -le 1 ];then
                    para_check=1
                    echo "After the parameter -f, must set the filename."
                fi
                if [ "${para_check}" -eq 0 ] && [ "${param}" == "-p" ];then
                    eval sudo /usr/bin/os_backup_info.sh $L_PARAM
                    backup_ret="$?"
                    if [ "${backup_ret}" -eq 0 -a -z "`echo "${L_PARAM}" | grep "[[:space:]]\-p[[:space:]]*$" 2>/dev/null`" ] || [ "${backup_ret}" -eq 2 ];then
                        echo "The log collection is finished, you can get the log from the directory /OSM/coffer_data/omm/export_import/."
                    fi
                elif [ "${para_check}" -eq 0 ] && [ "${param}" != "-p" ];then
                    L_PARAM=${L_PARAM}${L_PARAM1}
                    eval sudo /usr/bin/os_backup_info.sh $L_PARAM
                    backup_ret="$?"
                    if [ "${backup_ret}" -eq 0 -a -z "`echo "${L_PARAM}" | grep "[[:space:]]\-p[[:space:]]*$" 2>/dev/null`" ] || [ "${backup_ret}" -eq 2 ];then
                        echo "The log collection is finished, you can get the log from the directory /OSM/coffer_data/omm/export_import/."
                    fi
                fi
                ;;
            #SR-0000288844-支持小系统日志收集 added on 2013-10-18 end
            #AR-0000762469-支持6U4C的2扩4方案 added on 2014-05-22 begin
            "os_change_bayconfig.sh")
                L_BAY_PARAM=${G_CMD_PARAM#os_change_bayconfig.sh}
                eval sudo /OSM/script/os_change_bayconfig.sh $L_BAY_PARAM
                ;;
            #AR-0000762469-支持6U4C的2扩4方案 added on 2014-05-22 end
            "os_upgrade_hotpatch.sh")
	            if [ "`$G_REALPATH_CMD /startup_disk/image/boot`" != "/startup_disk/image/boot_osp" ];then
	                echo "The system is in rescue mode. After the \"resumebootmode\" command is executed successfully, run the \"os_upgrade_hotpatch.sh\" command."
	                continue
	            fi
                L_PARAM=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                L_HOTPATCH_PARAM=${G_CMD_PARAM#os_upgrade_hotpatch.sh}
                eval sudo setsid /OSM/script/os_upgrade_hotpatch.sh $L_HOTPATCH_PARAM
                ;;
            "os_write_bay_cfg.sh")
				L_BAYCFG_PARAM=${G_CMD_PARAM#os_write_bay_cfg.sh}
                m_ret=$(echo ${L_BAYCFG_PARAM} | grep -w "\-m")
				r_ret=$(echo ${L_BAYCFG_PARAM} | grep -w "\-r")
                if [ -n "$m_ret" -o -n "$r_ret" ]; then
                    eval sudo /OSM/script/os_write_bay_cfg.sh -h
                else
                    eval sudo /OSM/script/os_write_bay_cfg.sh $L_BAYCFG_PARAM
                fi
				;;
            "sysforcepoweron")   
                L_PARAM_NUM=`echo "$G_CMD_PARAM" | awk '{print NF}'`
                if [ ${L_PARAM_NUM} -ne 2 ];then
                    echo 'Input parameter number error!'
                    echo "Try 'help' for more information."
                    continue
                fi
                L_SWITCH_FLAG=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                if [ "${L_SWITCH_FLAG}" != "0" -a "${L_SWITCH_FLAG}" != "1" ];then
                   echo "Input parameter invalid!"
                   echo "Try 'help' for more information."
                   continue
                fi
                L_INSMOD_OSPSYS=`lsmod |awk '{print $1}' | grep 'sysctrl'`
                if [ -z "$L_INSMOD_OSPSYS" ] ; then
                    echo 'The command do not execute.'
                    continue
                fi
                echo -e "sys setforcepoweronswitch ${L_SWITCH_FLAG}\nexit\n" | diagnose --auto
                ;;
            "changeibckey")
                sudo /OSM/script/kmm_encrypt_ssh.sh update
                ;;
	    "telnet")
	        L_PARAM_NUM=`echo "$G_CMD_PARAM" | wc -w`
		if  [ $L_PARAM_NUM -ne 3 ];then
		    echo "paramers is not 2,please check your command"
		    echo "telnet IPv4|IPv6 port(1~65535):diagnose client telnet server status."
		    continue
		fi
		L_PARAM_IP=`echo "$G_CMD_PARAM" |awk '{print $2}'`
		ipv4_flag=`echo $L_PARAM_IP | grep -E "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5] |2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"`
		if [ -z "$ipv4_flag" ];then
		    ipv6_flag=`echo $L_PARAM_IP | grep -E "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9{1}]|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})(\.(25[0-5]|2[0-4][0-9]{1}|1[0-9]{1}[0-9]{1}|[1-9]?[0-9]{1})){3}))|:)))(%.+)?\s*$"`
	            if [ -z "$ipv6_flag" ];then
		        echo "input ip is not a invalid IPv4 or IPv6 address."
			continue
		    fi
		fi
		L_PARAM_Port=`echo "$G_CMD_PARAM" |awk '{print $3}'|grep -E "^[0-9]{1,5}$"`
		if [ -z "$L_PARAM_Port" ];then
		    echo "client_port_num must be an integer,range is 1~65535,please check your input."
		    continue
		else
		    if [ $L_PARAM_Port -le 65535 -a $L_PARAM_Port -ge 1 ];then
		        sudo $G_CMD_PARAM
		    else
		        echo "client_port_num must be an integer,range is 1~65535,please check your input."
		        continue
	            fi
		fi
		;;
		"df")
                L_PARAM_VAL=`echo "$G_CMD_PARAM" | awk '{print $2}'`
                L_PARAM_NUM=`echo "$G_CMD_PARAM" | wc -w`
                if [ "${L_PARAM_VAL}" == "-h" -a ${L_PARAM_NUM} -eq 2 ];then
                    sudo df -h
                else
                    echo "The command \"${G_CMD_PARAM}\" is invalid. Run \"df -h\"."
                fi
                ;;
        "enableiocfw")
        echo "DANGER: You are about to perform a danger command. After executing this command, restart controller will update the firmware of all Hi1821 cards on this controller. You must run this command on each controller."
        echo "Suggestion: After you perform this operation, you must restart the device to take effect, but which will affect working of system."
        echo "Are you sure to update firmware of Hi1821 on this controller?(y/n)"
        read -p "" L_INPUT
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
                        if [ -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag ] ; then
                            echo "[hi1821] ------The firmware is already update."	
                        else
                            if [ -f /startup_disk/conf/conf_local/ResumeiocFwFlagSPH007Flag ] ; then
                                rm -f /startup_disk/conf/conf_local/ResumeiocFwFlagSPH007Flag
                                if [ $? -ne 0 ]; then
                                    echo "[hi1821] ------rm /startup_disk/conf/conf_local/ResumeiocFwFlagSPH007Flag file failed."
                                    exit 0
                                fi
                            fi
                            
                            touch -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag
                            if [ $? -eq 0 ]; then
                                echo "[hi1821] ------The firmware upgrade of the Hi1821 card takes effect only after the controller is restarted, please reboot this controller."
                            else
                                echo "[hi1821] ------touch /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag file failed."
                            fi
                            sync
                        fi
                fi
                ;;    
    
        "disableiocfw")
        echo "DANGER: You are about to perform a danger command. After executing this command, restart controller will resume the firmware of all Hi1821 cards on this controller. You must run this command on each controller."
        echo "Suggestion: After you perform this operation, you must restart the device to take effect, but which will affect working of system."
        echo "Are you sure to resume firmware of Hi1821 cards on this controller?(y/n)"
        read -p "" L_INPUT        
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then               
                        if [ -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag ] ; then
                            rm -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag
                            if [ $? -eq 0 ]; then
                                touch -f /startup_disk/conf/conf_local/ResumeiocFwFlagSPH007Flag
                                if [ $? -eq 0 ]; then
                                    echo "[hi1821fw] ------The firmware resume of the Hi1821 card takes effect only after the controller is restarted, please reboot this controller."
                                else
                                    echo "[hi1821fw] ------touch /startup_disk/conf/conf_local/ResumeiocFwFlagSPH007Flag file failed."
                                fi
                                sync
                            else
                                echo "[hi1821fw] ------rm /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag file failed."
                            fi
                        else
                            echo "[hi1821fw] ------The firmware no need to resume."
                        fi
                fi
                ;;
#DTS2021083021265 增加控制器故障灯灭灯命令,增加SES固件升级回退命令，     
	 "enablesesfw")
        echo "DANGER: You are about to perform a danger command. After executing this command, restart controller will update the firmware of all manage_chip on this controller. You must run this command on each controller."
        echo "Suggestion: After you perform this operation, you must restart the device to take effect, but which will affect working of system."
        echo "Are you sure to update firmware of manage_chip on this controller?(y/n)"
		BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`" 
		 if [[ "$BOARD_TYPE" != PANGEA_V2R1C00  && "$BOARD_TYPE" != PANGEA_V2R2C10 ]]; then
         echo "[failed] ------Board type ($BOARD_TYPE) mismatch."
		 else
		 OS_echo "[Success] ------Board type ($BOARD_TYPE) match."
         read -p "" L_INPUT
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
                        if [ -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag ] ; then
                            echo "[success] ------The firmware is already update."  
                        else
                            if [ -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag ] ; then
                                rm -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag
                                if [ $? -ne 0 ]; then
                                    echo "[failed] ------rm /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag file failed."
                                    exit 0
                                fi
                            fi
                            
                            touch -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag
                            if [ $? -eq 0 ]; then
                                echo "[success] ------The firmware upgrade of the manage_chip takes effect only after the controller is restarted, please reboot this controller."
                            else
                                echo "[failed] ------touch /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag file failed."
                            fi
                            sync
                        fi
             
			    fi
         fi
                ;;    
    "disablesesfw")
        echo "DANGER: You are about to perform a danger command. After executing this command, restart controller will update the firmware of all manage_chip on this controller. You must run this command on each controller."
        echo "Suggestion: After you perform this operation, you must restart the device to take effect, but which will affect working of system."
        echo "Are you sure to resume firmware of manage_chip on this controller?(y/n)"
	    BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`" 
		if [[ "$BOARD_TYPE" != PANGEA_V2R1C00  && "$BOARD_TYPE" != PANGEA_V2R2C10 ]]; then
        echo "[failed] ------Board type ($BOARD_TYPE) mismatch."
		else
		OS_echo "[Success] ------Board type ($BOARD_TYPE) match."
        read -p "" L_INPUT        
                if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then               
                        if [ -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag ] ; then
                            rm -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag
                            if [ $? -eq 0 ]; then
                                touch -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag
                                if [ $? -eq 0 ]; then
                                    echo "[sesfw] ------The firmware resume of the manage_chip takes effect only after the controller is restarted, please reboot this controller."
                                else
                                    echo "[sesfw] ------touch /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag file failed."
                                fi
                                sync
                            else
                                echo "[sesfw] ------rm /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag file failed."
                            fi
                        else
                            echo "[sesfw] ------The firmware no need to resume."
                        fi
                fi
		fi				
             ;;
            "")
                ;;
             *)
                L_PWD=`pwd`
                # 检查命令合法性
                OS_CHECK_CMD_VALID
                if [ "$?" != 0 ];then
                    cd ${L_PWD} &>/dev/null
                    continue
                fi       

                
                # 高危命令执行提示
                OS_ALARM_CMD_EMERGENCY
                if [ $? -ne 0 ]; then
                    echo "DANGER: You are going to perform a danger command."
                    echo "Suggestion: Before you perform this operation, make sure the operation will not affect working of system."
                    echo "Have you read danger alert message carefully?(y/n)"
                    read -p "" L_INPUT
                    if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
                        echo "Are you sure you really want to perform the operation?(y/n)"
                        read -p "" L_INPUT
                        if [ "${L_INPUT}" != "y" -a "${L_INPUT}" != "Y" ]; then
                            cd ${L_PWD} &>/dev/null
                            continue
                        fi
                    else
                        cd ${L_PWD} &>/dev/null
                        continue
                    fi
                fi
               
                #白名单防呆处理，增加验证码输入与高危提示
                CLI_VERIFICATION_AND_DANGER
                if [ $? -ne 1 ]; then
                  cd ${L_PWD} &>/dev/null
                  continue
                fi
          
                #执行命令
                OS_EXECUTE_DEBUG_CMD
                if [ "$?" != 0 ];then
                    cd ${L_PWD} &>/dev/null
                    continue
                fi
                ;;
    esac
done

exit 0


