usassh免费SSH代理自动登录脚本

USAssh.com 最早成立于2009年,是一家注重用户体验、高速稳定、诚信专业、老牌资深的美国SSH代理服务提供商,专注于给用户提供优质稳定的SSH代理和VPN代理服务。

2012-7-23 update:更新ruby获取密码脚本!

最近发现usassh提供的有免费测试,美中不足的是每整点更换密码。经过摸索自己写了个脚本实现自动抓取密码并自动重新连接。(如果您觉得服务不错,请购买付费账户,也算支持别人的工作 🙂 )

脚本使用了工具expect,Ubuntu用户可以用sudo apt-get install expect安装。

1.expect脚本内容如下,主要实现自动登录的处理。

[crayon lang=”sh” title=”sshlogin”]
#! /usr/bin/expect
#
# sshlogin by yemingtu
set timeout -1
# 从参数中获取用户名和密码
set login_name [lindex $argv 0]
set login_pass [lindex $argv 1]
# 免费SSH服务器
set ssh_server “free.usassh.com”
# 新建进程ssh隧道开启9000端口用于动态转发,-o StrictHostKeyChecking=no参数略过缓存key提示
spawn ssh -D 9000 -g -N -p 80 -C -o StrictHostKeyChecking=no $login_name@$ssh_server
# 自动输入密码
expect “*password*”
send “$login_pass\r”
# 密码错误退出脚本
expect “*again*”
exit 0
interact
[/crayon]

2.启动SSH状态检测脚本
[crayon lang=”sh” title=”autossh”]
#! /bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# php写的抓密码页面,返回最新密码
url=”http://hmmabc.com/ssh.php”
# 检测ssh进程是否存在,如果存在则退出
if [ -n “$(ps aux|grep “ssh -D 9000″|grep -v grep)” ]
then
exit 0
fi
password=`curl $url`
# 检测获取的密码是否为空
if [ -z “$password” ]
then
exit 0
fi
# 调用上面的expect脚本登录
/usr/bin/expect /home/yourname/ssh/sshlogin usassh $password
[/crayon]

3.将上面两个脚本保存到文件,比如/home/yourname/ssh目录,并用chmod +x * 加上可执行权限。

4.用crontab -e命令编辑cron任务,每分钟执行一次autossh脚本

0-59 * * * * /home/yourname/ssh/autossh &

最后附上自己写的php抓取密码脚本,非常简单,仅供参考~ 🙂
[crayon lang=”php” title=”ssh.php”]
(.*?)

#”,$contents,$matches);
foreach ($matches[1] as $key => $value) {
if(strlen($value) == 5){
echo $value;
}
}
?>
[/crayon]

ruby获取密码脚本:(需要nokogiri)
[crayon lang=’ruby’ title=’getpwd.rb’]
require ‘nokogiri’
require ‘open-uri’

uri = ‘http://www.usassh.com/free.php’
doc = Nokogiri::HTML(open(uri))

doc.css(‘strong font’).each do |link|
if(link.content.length == 5)
puts link.content
end
end
[/crayon]

usassh免费SSH代理自动登录脚本》有一个想法

  1. 飒沓

    推荐一个多线程的ssh代理,迅雷般的速度,http://www.myssh.org

评论已关闭。