当前位置: 主页 > 业内 >

使用 autossh 自动重启 SSH 会话和通道

时间:2013-05-29 09:52来源:www.oschina.net 作者:admin 点击:
autossh 是一个用来启动 ssh 并进行监控的程序,可在需要时重启 ssh,例如程序挂掉或者是网络出现问题。其灵感和机制来自于 rstunnel (Reliable SSH Tunnel). autossh 1.2 的方法已经改变:autossh
欢迎大家分享自己的文档,请点击查阅:分享方法,Linux系统运维
如果你喜欢这文章,可以点击文章结尾处百度分享,分享到你的各种社区收藏,或推荐给朋友……


autossh 是一个用来启动 ssh 并进行监控的程序,可在需要时重启 ssh,例如程序挂掉或者是网络出现问题。其灵感和机制来自于 rstunnel  (Reliable SSH Tunnel). autossh 1.2 的方法已经改变:autossh 使用 ssh 来构造一个 ssh 重定向循环(本地到远程和远程到本地),然后发送测试数据并获得返回结果。 

autossh 1.3 增加了一个新的方法:可指定远程 echo 服务的端口用于返回测试数据发送结果。这个避免握手阶段以及所有远程机器端口的冲突问题。而老的 loop-of-forwardings 方法依然可用。

在 Ubuntu 上安装 autossh

打开终端窗口并运行以下命令

sudo apt-get install autossh

Autossh 语法

autossh [-V] [-M port[:echo_port]] [-f] [SSH_OPTIONS]

在启动时启动 ssh 通道

我们可以使用 upstart 来在 Ubuntu 下启动 ssh 通道,只需要将如下 autossh.conf 文件放在 /etc/init 目录即可。

# autossh startup Script

description "autossh daemon startup"

start on net-device-up IFACE=eth0
stop on runlevel [01S6]

respawn
respawn limit 5 60 # respawn max 5 times in 60 seconds

script
export AUTOSSH_PIDFILE=/var/run/autossh.pid
export AUTOSSH_POLL=60
export AUTOSSH_FIRST_POLL=30
export AUTOSSH_GATETIME=0
export AUTOSSH_DEBUG=1
autossh -M 0 -4 -N USER@HOSTNAME -D 7070 -o "ServerAliveInterval 60″ -o "ServerAliveCountMax 3″ -o BatchMode=yes -o StrictHostKeyChecking=no -i SSH_KEY_FILE_PATH
end script


项文原始资料:http://www.ubuntugeek.com/automatically-restart-ssh-sessions-and-tunnels-using-autossh.html

utossh is a program to start a copy of ssh and monitor it, restarting it as necessary should it die or stop passing traffic.The original idea and the mechanism were from rstunnel (Reliable SSH Tunnel). With version 1.2 of autossh the method changed: autossh uses ssh to construct a loop of ssh forwardings (one from local to remote, one from remote to local), and then sends test data that it expects to get back.

With version 1.3, a new method is added (thanks to Ron Yorston): a port may be specified for a remote echo service that will echo back the test data. This avoids the congestion and the aggravation of making sure all the port numbers on the remote machine do not collide. The loop-of-forwardings method remains available for situations where using an echo service may not be possible.

 

Install autossh in ubuntu

Open the terminal and run the following command

sudo apt-get install autossh

Autossh syntax

autossh [-V] [-M port[:echo_port]] [-f] [SSH_OPTIONS]

Start ssh tunnel at boot time

We can use upstart to start ssh tunnel under Ubuntu by put the following autossh.conf file under /etc/init/ folder

# autossh startup Script

description "autossh daemon startup"

start on net-device-up IFACE=eth0
stop on runlevel [01S6]

respawn
respawn limit 5 60 # respawn max 5 times in 60 seconds

script
export AUTOSSH_PIDFILE=/var/run/autossh.pid
export AUTOSSH_POLL=60
export AUTOSSH_FIRST_POLL=30
export AUTOSSH_GATETIME=0
export AUTOSSH_DEBUG=1
autossh -M 0 -4 -N USER@HOSTNAME -D 7070 -o "ServerAliveInterval 60″ -o "ServerAliveCountMax 3″ -o BatchMode=yes -o StrictHostKeyChecking=no -i SSH_KEY_FILE_PATH
end script

转载请注明linux系统运维
http://www.linuxyw.com/linux/yenei/2013/0529/494.html

------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
验证码: 点击我更换图片