自动登录测试环境脚本-V2版

自动登录测试环境脚本

由于我们可能需要登录多套测试环境,每套测试环境都可能有多个机器,所以最好是能有一个自动登录的脚本可以自由的切换到不同的环境的不同机器上。

1. 文件结构

ericte@pl22b16v1-0:/mnt/sharedDrive/ericte/autoLogin$ ls -l
total 12
-r-xr-xr-x 1 ericte rnd 2623 Dec  2 09:28 autoLogin.sh
-r-xr-xr-x 1 ericte rnd  409 Dec  2 08:36 login.exp
drwxrwxr-x 2 ericte rnd 4096 Dec  2 10:15 servers
ericte@pl22b16v1-0:/mnt/sharedDrive/ericte/autoLogin$ ls -l servers/
total 12
-rw-rw-r-- 1 ericte rnd 185 Dec  2 08:45 CI-POD01
-rw-rw-r-- 1 ericte rnd 247 Dec  2 08:36 HYB5
-rw-rw-r-- 1 ericte rnd 199 Dec  2 10:15 HYB2

2. 测试环境文件

ericte@pl22b16v1-0:/mnt/sharedDrive/ericte/autoLogin$ cat servers/CI-POD01
MN 137.58.243.68
DDC1 137.58.243.71
DDC2 137.58.243.72

3. autoLogin.sh

#!/usr/bin/env bash
#===============================================================================
#
#          FILE: autoLogin.sh
#
#         USAGE: ./autoLogin.sh
#
#   DESCRIPTION: auto login test environment
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: 哼!无知是恶
#  ORGANIZATION: 博客园
#       CREATED: 12/02/20 04:36:58
#      REVISION:  v1
#===============================================================================

set -o nounset                              # Treat unset variables as an error

date=$(date +'[%F %T]')
color_end='\e[0m'
color_red='\e[1;31m'
color_green='\e[1;32m'
color_yellow='\e[1;33m'
color_skyblue='\e[1;36m'
CWD="$HOME/autoLogin"
autoScript="$CWD/login.exp"

cd ${CWD}

function initTestChannel() {
  server=$1
  numberOfNode=$(cat $server | wc -l)
  for((i=1; i<$(($numberOfNode+1));i++))
  do
    name=$(sed -n "${i}p" $server | awk '{print $1}')
    ip=$(sed -n "${i}p" $server | awk '{print $2}')
    acc=$(sed -n "${i}p" $server | awk '{print $3}')
    pwd=$(sed -n "${i}p" $server | awk '{print $4}')
    array_index=$(($i-1))
    hostname[$array_index]=$name
    ipaddr[$array_index]=$ip
    account[$array_index]=$acc
    passwd[$array_index]=$pwd
  done
}

function initFile() {
  numberOfFile=$(ls -l servers/* | wc -l)
  for((j=1;j<$(($numberOfFile+1));j++))
  do
    file=$(ls -l servers/* | sed -n "${j}p" | awk '{print $9}')
    file_index=$(($j-1))
    file_array[$file_index]=$file
  done
}

function promotionFileMessage()
{
  length=${#file_array[@]}
  for((i=0;i<$length;i++))
  do
    fileName=$(echo ${file_array[$i]} | awk -F '/' '{print $2}')
    echo -e "$color_red$i: $fileName$color_end"
  done
}

function promotionEnvMessage() {
  length=${#hostname[@]}
  for((k=0;k<$length;k++))
  do
    echo -e "$color_red$k: ${hostname[$k]} ${ipaddr[$k]}$color_end"
  done
}

initFile
echo -e "Do you want to login?"
promotionFileMessage
read -t 15 -p "Plese input your selection(number): " testchannel

initTestChannel ${file_array[$testchannel]}

name=$(echo ${file_array[$testchannel]} | awk -F '/' '{print $2}')
echo -e "welcome to test channel $color_red${name}$color_end"
promotionEnvMessage

read -t 15 -p "please input your selection(number): " serverOption
case "$serverOption" in
     [0-9]|1[0-9]|2[0-9])
        echo -e "$date: Welcome to host ${color_red}${hostname[$serverOption]}${color_end}"
        $autoScript ${ipaddr[$serverOption]} ${account[$serverOption]} ${passwd[$serverOption]}
        ;;
     *)
        echo -e "\n${color_red}Error:${color_end} Input timeout or your input is invalid, exited\n"
        exit 1
        ;;
esac

4. expect脚本

#!/usr/bin/expect -f
# set Variables
set ipaddr [lrange $argv 0 0]
set account [lrange $argv 1 1]
set password [lrange $argv 2 2]
set timeout 2
spawn ssh -q $account@$ipaddr
#match_max 100000
# Look for passwod prompt
expect {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
exp_continue
}
"*?assword:*" {
send "$password\r"
exp_continue
}
eof
{
        send_user "eof\n"
}
}
interact
posted @ 2020-12-02 18:00  无知是恶  阅读(175)  评论(0编辑  收藏  举报