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

July 6, 2010

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

tags: , ,
posted in Shell, Unix/Linux by PT

Follow comments via the RSS Feed | Leave a comment | Trackback URL

6 Responses to Across the Great Wall we can reach every corner in the world

  1. chenxiaoqino says:

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

  2. Pingback: Freessh.us的自动连接脚本(Python) | Felix's Blog

  3. Pingback: Freessh.us的自动连接脚本(Python) | My Sky

  4. very says:

    呵呵,佩服

  5. ghxandsky says:

    "sudo apt-get install expext or sudo yum install expect"

    shuold be:

    "sudo apt-get install expect or sudo yum install expect"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Page optimized by WP Minify WordPress Plugin

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org