[工具]使用 autossh 工具保持ssh连接不断线

autossh 介绍

autossh 是一种用于在 Linux主机之间建立持久性的 SSH 隧道的工具。它是 ssh 的一种包装器,用于在不稳定的网络环境下保持 ssh 连接。在网络不稳定的情况下,ssh 连接可能会断开,autossh 可以在连接丢失时重新连接,保持隧道处于活动状态。

在 ssh 命令基础上增加防断线功能, 支持 ssh 命令中所有参数配置 , 并额外增加新的参数

  1. -M 参数用于指定监听端口, 若指定为 0表示为不监听
  2. -f 以后台方式运行

autossh 安装

ubutun 系统

sudo apt-get install autossh

centos 系统

yum install autossh

mac 系统

brew isntall autossh

autossh 基本用法

打开命令行终端 输入 autossh -v

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

使用示例

将本地主机的 80 端口转发到远程主机的 8080 端口

在本地主机上启动 autossh 进行端口转发。假设本地主机的 IP 地址为 localhost,需要将本地主机的 80 端口转发到远程主机的 8080 端口。启动命令如下:

autossh -M 20000 -N -R 8080:localhost:80 user@remotehost

其中 -M 参数用于指定 autossh 的监听端口, -N 参数表示不打开 SSH 会话,只进行端口转发。 -R 参数用于指定远程主机的端口转发规则,格式为:远程端口:本地主机IP:本地主机端口。在本例中,远程主机的 8080 端口将被映射到本地主机的 80 端口上。
在远程主机上访问 http://localhost:8080,应该能够正常显示本地主机上的内容。

将远程主机的 80 端口转发到本地主机的 8080 端口

在本地主机上启动 autossh 进行端口转发。假设本地主机的 IP 地址为 localhost,需要将远程主机的 80 端口转发到本地主机的 8080 端口。启动命令如下:

autossh -M 20000 -N -L 8080:localhost:80 user@remote_server_ip

远程主机上访问 http://localhost:80,应该能够正常显示远程主机上的内容。

脚本封装

#!/bin/bash
AUTOSSH_LOGFILE=/var/log/autossh.log
AUTOSSH_POLL=30
AUTOSSH_FIRST_POLL=60
AUTOSSH_GATETIME=0
AUTOSSH_PORT=0
AUTOSSH_DEBUG=yes
export AUTOSSH_LOGFILE AUTOSSH_POLL AUTOSSH_FIRST_POLL AUTOSSH_GATETIME AUTOSSH_PORT AUTOSSH_DEBUG
case "$1" in
 start)
  autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -f -R 8080:localhost:80 user@remotehost
  ;;
 stop)
  killall -TERM autossh
  ;;
 restart)
  killall -TERM autossh
  autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -f -R 8080:localhost:80 user@remotehost
  ;;
 *)
  echo "Usage: $0 {start|stop|restart}"
  exit 1
  ;;
esac
exit 0

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值