#!/bin/sh function trapper(){ trap 'exit 1' EXIT QUIT; } serverArr=( guard-boot-001,10.1.17.12 guard-boot-002,10.1.17.13 guard-boot-003,10.1.18.231 guard-boot-004,10.1.18.232 ); function printServers(){ echo '===================================SERVERS==================================='; for ((i=0; i< ${#serverArr[*]}; i++)) do serverName=$(echo ${serverArr[$i]}|cut -d ',' -f 1); serverIP=$(echo ${serverArr[$i]}|cut -d ',' -f 2); echo -e "${i}\t${serverName} \t\t\t ${serverIP}"; done echo '===================================++END++==================================='; } function conn(){ index=$1; for ((i=0; i<${#serverArr[*]}; i++)) do if [ $index -eq $i ] then serverIP=$(echo ${serverArr[$i]}|cut -d ',' -f 2); fi done; echo "Connecting to $serverIP"; ssh appweb@$serverIP; } function main(){ while true do trapper; clear; printServers; read -p '输入服务器编号' num; conn $num; done; } main;