shell数组
1 group='www_ wwwhuidu_' 2 echo "*********************" 3 for i in $group 4 do 5 echo "* $a,$i组" 6 list2[$a]=$i 7 a=`expr $a + 1` 8 done 9 echo "* $a,其他" 10 echo "*********************" 11 i=0 12 read -p "请输入编号: " number 13 if [ "$number" == "" ];then 14 ip 15 fi 16 } 17 18 function ip() { 19 while true 20 do 21 menu 22 if [ "$number" == "$a" ];then 23 group2=`echo $group | sed 's/ /|^/g'` 24 for ip in `awk -F',' '{print $2"---"$7"---"$8}' ip.csv| sed 's/\"//g' | sort | egrep -v "$group2"` 25 do 26 ip_list=`echo $ip | awk -F'---' '{print $2}'` 27 list[$i]=$ip_list 28 echo "($i) $ip" 29 i=`expr $i + 1` 30 done 31 echo "($i) 回上一级" 32 else 33 for ip in `awk -F',' '{print $2"---"$7"---"$8}' ip.csv| sed 's/\"//g' | sort | grep ${list2[$number]}` 34 do 35 ip_list=`echo $ip | awk -F'---' '{print $2}'` 36 list[$i]=$ip_list 37 echo "($i) $ip" 38 i=`expr $i + 1` 39 done 40 echo "($i) 回上一级" 41 fi 42 read -p "输入编号: " number 43 if [ "$number" == "$i" ] || [ "$number" == "" ];then 44 ip 45 fi 46 expect exp/sshlogin.exp ${list[$number]} passport 22 salt 1 47 done 48 } 49 50 ip 51 52 [root@localhost saltnew]# cat ip.csv 53 "实例ID","名称","主机名","描述","Region名称","所在可用区","公网IP","内网IP","CPU","内存","付费类型","网络类型","带宽","创建时间(UTC)","到期时间(UTC)" 54 "i-23lw8l8kk","beta_bbs","xxx","","cn-hangzhou","cn-hangzhou-b","1.1.1.1","10.1.1.1","8","8192","包年包月","经典网络","1","2016-01-11T02:00Z","2016-02-11T16:00Z"
1 #!/usr/bin/expect 2 set ip [lindex $argv 0] 3 set pass [lindex $argv 1] 4 set port [lindex $argv 2] 5 set user [lindex $argv 3] 6 set id_dsa [lindex $argv 4] 7 8 if { $id_dsa == "1" } {set id "exp/wdzj_key";set i "-i" 9 spawn ssh $i $id -p $port $user@$ip 10 expect { 11 "*(yes/no)?" {send "yes\r"; exp_continue} 12 "*password:" { send "$pass\r"} 13 "*wdzj_key':" { send "$pass\r"} 14 } 15 } 16 if { $id_dsa == "0" } { 17 spawn ssh -p $port $user@$ip 18 expect { 19 "*(yes/no)?" {send "yes\r"; exp_continue} 20 "*password:" { send "$pass\r"} 21 } 22 } 23 interact
这一例子想表示的就是shell数组的使用方法及用途!
效果如下:
*********************
* 0,www_组
* 1,wwwhuidu_组
* 21,其他
*********************
请输入编号: