2010-7-26 Updated: 支持侦测繁忙服务器。
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #!/bin/bash LOCAL_PORT=7070 auto_login_ssh () { expect -c "set timeout -1; spawn ssh -o StrictHostKeyChecking=no $2 ${@:3}; expect *assword:*; send -- $1\r; interact;"; } conn_to_ssh () { echo "Retriving servers list from freessh.us ...." HOSTS=($(curl freessh.us | sed -n "s/.*<td bgcolor='#FFFFFF'>\(.\+\?\)<\/td>.*$/\1/p")) ITMS=${#HOSTS[@]} for I in seq 0 7 $(($ITMS - 1)); do if [[ ${HOSTS[$I + 6]} == "正常" ]]; then IDX=$I break elif [[ $I -eq $(($ITMS - 7)) ]]; then echo -e "\nError: All server are busy, or something's wrong: \n\n${HOSTS[@]}" exit 1 fi done HOST=${HOSTS[1+$IDX]} USR=${HOSTS[2+$IDX]} PSW=${HOSTS[3+$IDX]} echo "Connecting to ${HOSTS[0+$IDX]} $HOST" auto_login_ssh $PSW $USR@$HOST -NC -D $LOCAL_PORT } while true; do conn_to_ssh done |
Remember to have expect installed:
1 | sudo apt-get install expect |
or
1 | sudo yum install expect |
Save the code above or download source from google code:
1 2 | wget http://ptcoding.googlecode.com/svn/trunk/freessh.sh chmod +x freessh.sh |
To get CROSSED, just run ./freessh.sh
or put this into your own PATH.
感谢国家,感谢freessh.us
早就想写这个脚本了,一直苦于不会写正则表达式弄不出来…
感谢国家!
呵呵,佩服
“sudo apt-get install expext or sudo yum install expect”
shuold be:
“sudo apt-get install expect or sudo yum install expect”
thx,真细心……