【1.4】shell基本实践——根据文件做交互选择

 

#!/bin/sh
function createFile()
{
        for f in "$@"; do
                if [ ! -f $f ]; then
                        touch $f
                        chmod 777 $f
                fi
        done
}



function init()
{
        dir=`pwd`
        mkdir -p ${dir}/data/iplist/
        #ipFileChina='/data/iplist/sq_iplist_china.txt'
        #ipFileAbroad='/data/iplist/sq_iplist_abroad.txt'
        #ipFileAbroadTest='/data/iplist/sq_iplist_abroad_test.txt'
       # ipFileAbroadKuafu='/data/iplist/sq_iplist_abroad_kuafu.txt'
       # ipFileAbroadOrder='/data/iplist/sq_iplist_abroad_order.txt'
        
        ipListFile="${dir}/data/iplist/iplist.txt"
        createFile ${ipListFile}
        #source /data/gameTools/config/toolsConfig.sh
        #----------------------------------------
        #mysql账户密码
        slaveUser='a'
        slavePwd='b'
        sshPwd='c'
}

function checkPwd()
{
        pwdCount=1
        read -s -p "Please input the password: " thePwd
        clear
        while true; do
                if [ "${pwdCount}" -ge 3 ]; then
                        echo "bye"
                        exit 1
                fi
                if [ "$thePwd" = "7roaddba" ]; then
                        break
                else 
                        echo "Error: The password is error."
                        echo 
                        read -s -p "Please input the password: " thePwd
                        clear
                fi
                pwdCount=`expr ${pwdCount} + 1`
        done
}
function autossh()
{
        SRC_HOST="$1"
        SRC_PORT="$2"
        SRC_PWD="$3"
        commands="$4"
        expect -c "
                set timeout 30
                spawn  ssh -p $SRC_PORT root@$SRC_HOST
                expect {
                          \"(yes/no)?\"                         {send \"yes\r\";exp_continue}
                          \"password:\"                         {send \"${SRC_PWD}\r\";exp_continue}
                          \"Last login\"                        {send \"$commands\r\";}
                       
                       }
                       interact
                       #expect eof
        "
}

function synIPdata()
{
       
        dataId="$1"
        saveFile="$2"
        tmpFile="`pwd`/tmp/sq_iplist_$RANDOM"
        if [ ! -d `dirname ${tmpFile}` ];then 
            mkdir `dirname ${tmpFile}`
        fi
        createFile $tmpFile
        echo -n "Sync ip list from main data......"
        mainDataUrl="this is a URL"
        curl "$mainDataUrl" -o $tmpFile >> /dev/null 2>&1
        if [ "$?" -ne 0 ]; then
                echo -e "\033[31;49;1mError: Some error occur when execute curl '$mainDataUrl' -o $tmpFile \033[39;49;0m"
                exit 1
        fi
        echo -e "\033[32;49;1mOK\033[39;49;0m" 
        #列表解析
        echo -n "Analyse $tmpFile......"
        cat $tmpFile | dos2unix | grep '|||' | sed 's/^ *//g' | sed 's/ /*/g' | perl -p -i -e "s/(/--(/g" | perl -p -i -e "s/\|\|\|/\n/g"  | tr '|' '\t'   | awk '{if ($1~/--(/) ; print $1,$2,$3,$4,$5,$6,$7,"@@"$1,$8} ' |  perl -p -i -e "s/@@.*--//g" |  awk '{if ($1~/--\(/) gsub("--(", "*");sub("合区)", "");print $0} ' | sed 's/@@//g' | sed '/^\s*$/d' | sed '/^\s*#/d' > $saveFile
        if [ "$?" -ne 0 ]; then
                echo -e "\033[31;49;1mError: Some error occur when analyse $tmpFile \033[39;49;0m"
                exit 1
        fi
        echo -e "\033[32;49;1mOK\033[39;49;0m" 
        rm -f $tmpFile
}
function sshIp()
{
        autossh ${the_outer_ip} 16333 ${sshPwd} "export PS1='\\\u@\\\\\[\\\e\[0;31m\\\\\]<${theSiteId}-${theServerName}>\\\\\[\\\e\[m\\\\\]:\\\\\\$'"
}


function serverList()
{
        i=1
        createFile "${dir}/tmp/theServerList"
        cat ${ipListFile} |awk '{print $1}' |sort -u| while read theServer; do
                echo "$((i++)) - ${theServer}"
        done > ${dir}/tmp/theServerList
        echo
        echo " Server Select"
        echo "------------------------------"
        cat ${dir}/tmp/theServerList
        echo
        echo "R - Return"
        echo "------------------------------"
        echo -n "Please chose the agent number : "
        read theServerNum
        while true; do
                if [ "${theServerNum}" = "R"  -o  "${theServerNum}" = "r" ]; then
                        break
                fi
                isSelectOK=`cat ${dir}/tmp/theServerList | grep -E "^${theServerNum} - " | wc -l`
                if [ "${isSelectOK}" -ne 0 ]; then
                        break
                else
                        echo "Error: No such server number."
                        echo
                        echo -n "Please chose the server number: "
                        read theServerNum
                fi
        done
        theServerName=`cat ${dir}/tmp/theServerList | grep "^${theServerNum} - " | awk -F" - " '{print $2}'`
}

function serverList1()
{        if [ -n $1 ];then
            theServerName1=$1
        else
            echo 'The function serverList1 have not input variables!'
        fi
        
        i=1
        TemptheServerName=`echo ${theServerName}|tr '*' 'a'`
        createFile "${dir}/tmp/theServerList1"
        cat ${ipListFile}|tr '*' 'a' |grep "${TemptheServerName}"|awk '{print $2}' |sort -u| while read theServer1; do
                echo "$((i++)) - ${theServer1}"
        done > ${dir}/tmp/theServerList1
        echo
        echo " Server Select"
        echo "------------------------------"
        cat ${dir}/tmp/theServerList1
        echo
        echo "R - Return"
        echo "------------------------------"
        echo -n "Please chose the agent number : "
        read theServerNum1
        while true; do
                if [ "${theServerNum1}" = "R"  -o  "${theServerNum1}" = "r" ]; then
                        break
                fi
                isSelectOK=`cat ${dir}/tmp/theServerList1 | grep -E "^${theServerNum1} - " | wc -l`
                if [ "${isSelectOK}" -ne 0 ]; then
                        break
                else
                        echo "Error: No such server number."
                        echo
                        echo -n "Please chose the server number: "
                        read theServerNum1
                fi
        done
        theServerName1=`cat ${dir}/tmp/theServerList1 | grep "^${theServerNum1} - " | awk -F" - " '{print $2}'`
        the_outer_ip=`cat ${ipListFile}|tr '*' 'a' |grep "${TemptheServerName}"|grep "${theServerName1}"|awk '{print $3}'`
}




function main()
{
        if [ "$1" = "-h" -o "$1" = "--help" ]; then
                echo "sqlist [syncIP/syncChinaForCheck/syncAbroadForCheck/syncAbroadOthers/syncAll] [toFile]"
                exit 0
        fi
        init
        #执行该工具只同步IP列表,不进入选区菜单
        synIPdata 6 ${ipListFile}
        checkPwd
        isOnServerList=0
        stty erase ^h
        while true; do
                if [ ${isOnServerList} -eq 0 ];then
                    serverList
                fi
                if [ "${theServerNum}" = "R"  -o  "${theServerNum}" = "r" ]; then
                        break
                else 
                    serverList1 ${theServerName}
                    if [ "${theServerNum1}" = "R"  -o  "${theServerNum1}" = "r" ]; then
                        isOnServerList=0
                        continue
                    fi
                fi
                
                sshIp
                
            
        done
}
main
posted @ 2020-01-16 10:36  郭大侠1  阅读(355)  评论(0编辑  收藏  举报