Across the Great Wall we can reach every corner in the world

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

文章分类 Shell, Unix/Linux 标签: , ,
4 comments on “Across the Great Wall we can reach every corner in the world
  1. chenxiaoqino说道:

    早就想写这个脚本了,一直苦于不会写正则表达式弄不出来…
    感谢国家!

  2. very说道:

    呵呵,佩服

  3. ghxandsky说道:

    “sudo apt-get install expext or sudo yum install expect”

    shuold be:

    “sudo apt-get install expect or sudo yum install expect”

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*