[摘要] 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
1
2
3
4
5
6
gunzip-cautossh-1.4c.tgz|tarxvf-
cdautossh-1.4c
./configure
make
copybinarytowhereyouwishit,or"make install"willinstallitunder/usr/localbydefault.
examineautossh.hostforexamplewrapperscriptandoptions
脚本用法
# ./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
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
# ./autossh
usage:autossh[-V][-Mmonitor_port[:echo_port]][-f][SSH_OPTIONS]
-Mspecifiesmonitorport.Maybeoverriddenbyenvironment
variableAUTOSSH_PORT.0turnsmonitoringloopoff.
Alternatively,aportforanechoserviceontheremote
machinemaybespecified.(Normallyport7.)
-fruninbackground(autosshhandlesthis,anddoesnot
passittossh.)
-Vprintautosshversionandexit.
Environmentvariablesare:
AUTOSSH_GATETIME-howlongmustansshsessionbeestablished
beforewedecideitreallywasestablished
(inseconds).Defaultis30seconds;useof-f
flagsetsthisto0.
AUTOSSH_LOGFILE-filetologto(defaultistousethesyslog
facility)
AUTOSSH_LOGLEVEL-leveloflogverbosity
AUTOSSH_MAXLIFETIME-setthemaximumtimetolive(seconds)
AUTOSSH_MAXSTART-maxtimestorestart(defaultisnolimit)
AUTOSSH_MESSAGE-messagetoappendtoechostring(max64bytes)
AUTOSSH_PATH-pathtosshifnotdefault
AUTOSSH_PIDFILE-writepidtothisfile
AUTOSSH_POLL-howoftentochecktheconnection(seconds)
AUTOSSH_FIRST_POLL-timebeforefirstconnectioncheck(seconds)
AUTOSSH_PORT-porttouseformonitorconnection
AUTOSSH_DEBUG-turnloggingtomaximumverbosityandlogto
stderr
下面是使用autossh登录服务器的列子:
./autossh -M port remote-ssh-server-IP
如:
./autossh -M 12345 sudops@172.32.5.239
1
2
3
./autossh-Mportremote-ssh-server-IP
如:
./autossh-M12345sudops@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
1
2
3
4
5
6
autossh-M12345-N-D-vlocalhost:7070sudops@172.32.5.239-p22
会看到如下监听端口:其中7070为本地
tcp00127.0.0.1:70700.0.0.0:*LISTEN7466/ssh
tcp00127.0.0.1:123450.0.0.0:*LISTEN7466/ssh
tcp00127.0.0.1:123460.0.0.0:*LISTEN7465/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.
1
2
3
4
Whennoechoserviceportisspecified,thisportandtheport
immediatelyaboveit(port# + 1) should be something nothing
elseisusing.autosshwillsendtestdataonthebasemonitoring
port,andreceiveitbackontheportabove.
怎么样,有意思不,赶快体验下传说中的“高大上” autossh 吧。