当前位置: 首页 > linux, 系统 > 正文

autossh 让你的 ssh 不掉线

[摘要] ssh是OPS及DEV同学登录服务器进行维护,部署等的必备工具。我们总会遇到长时间连接ssh后会自动断开需要重连的情况,之前我们都是使用screen来保证ssh不断线,现在有了一个更加给力的工具--autossh,autossh 是在ssh的基础之上,增加了一个监控端口,防止ssh session 过期,并可以重新连接保证不掉线,这保证我们在ssh执行远程服务器上的运行时间长的脚本时不会出现意外失联的情况。

下载地址:
download autossh-1.4c.tgz

源码安装,一次编译,到处执行:

gunzip -c autossh-1.4c.tgz | tar xvf -
cd autossh-1.4c
./configure
make
copy binary to where you wish it, or "make install" will install it under /usr/local by default.
examine autossh.host for example wrapper script and options

脚本用法

# ./autossh 
usage: autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS]

    -M specifies monitor port. May be overridden by environment
       variable AUTOSSH_PORT. 0 turns monitoring loop off.
       Alternatively, a port for an echo service on the remote
       machine may be specified. (Normally port 7.)
    -f run in background (autossh handles this, and does not
       pass it to ssh.)
    -V print autossh version and exit.

Environment variables are:
    AUTOSSH_GATETIME    - how long must an ssh session be established
                          before we decide it really was established
                          (in seconds). Default is 30 seconds; use of -f
                          flag sets this to 0.
    AUTOSSH_LOGFILE     - file to log to (default is to use the syslog
                          facility)
    AUTOSSH_LOGLEVEL    - level of log verbosity
    AUTOSSH_MAXLIFETIME - set the maximum time to live (seconds)
    AUTOSSH_MAXSTART    - max times to restart (default is no limit)
    AUTOSSH_MESSAGE     - message to append to echo string (max 64 bytes)
    AUTOSSH_PATH        - path to ssh if not default
    AUTOSSH_PIDFILE     - write pid to this file
    AUTOSSH_POLL        - how often to check the connection (seconds)
    AUTOSSH_FIRST_POLL  - time before first connection check (seconds)
    AUTOSSH_PORT        - port to use for monitor connection
    AUTOSSH_DEBUG       - turn logging to maximum verbosity and log to
                          stderr

下面是使用autossh登录服务器的列子:

./autossh -M port remote-ssh-server-IP       
如:              
./autossh -M 12345 sudops@172.32.5.239

创建ssh隧道的例子:

autossh -M 12345 -N -D -v localhost:7070 sudops@172.32.5.239 -p 22

会看到如下监听端口:其中7070为本地
tcp        0      0 127.0.0.1:7070            0.0.0.0:*     LISTEN      7466/ssh            
tcp        0      0 127.0.0.1:12345           0.0.0.0:*     LISTEN      7466/ssh            
tcp        0      0 127.0.0.1:12346           0.0.0.0:*     LISTEN      7465/autossh

能够看到autossh启动了一个echo service port 12345作为监控,另外还启动了一个port+1的端口12346作为接收端口,文档描述如下:

    When no echo service port is specified, this port and the port 
    immediately above it (port# + 1) should be something nothing 
    else is using. autossh will send test data on the base monitoring 
    port, and receive it back on the port above.
 

怎么样,有意思不,赶快体验下传说中的“高大上” autossh 吧。

本文固定链接: https://sudops.com/autossh-make-your-ssh-always-connected.html | 运维速度

该日志由 u2 于2014年05月14日发表在 linux, 系统 分类下,
原创文章转载请注明: autossh 让你的 ssh 不掉线 | 运维速度
关键字: ,

报歉!评论已关闭.