惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - centosboy

Thinkpad T490卡顿排查 xinference初探 diff的安装与使用 ubuntu docker运行大模型 vllm部署 zabbix监控mongodb /root/.dbshell无权限解决办法 开机启动后应用limit没有生效 - centosboy 关于运维工作的一点感悟 将CDLINUX装入U盘 hive分区表实践 hashlib模块 rm 防误删脚本 linux 通过inode删除文件 那些年踩过的坑 htpdate代替ntpdate同步时间 Openfire搭建聊天系统 zookeeper运维 centos ping添加丢包记录 Linux tee命令输出显示在屏幕和保存到日志文件中
Autossh打洞
centosboy · 2020-12-15 · via 博客园 - centosboy

autossh介绍:

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

内网主机主动连接到外网主机,又被称作反向连接(Reverse Connection),这样NAT路由/防火墙就会在内网主机和外网主机之间建立映射即可相互通信了。但这种映射是路由网关自动维持的,不会持续下去,如果连接断开或者网络不稳定都会导致通信失败,这时内网主机需要自动重连机制了。

安装:

yum install autossh

实例:

autossh -M 9090 -fCNR 18081:10.10.3.x:8080 root@x.x.x.x

autossh -M 9091 -fCNR 18082:10.10.3.x:80 root@x.x.x.x

autossh -M 9091 -fCNR 18083:127.0.0.1:80 root@x.x.x.x

这样将在'x.x.x.x'主机上开启一个本地侦听地址:18081,访问本地18081将转发至10.10.3.x:8080

  -M 9090参数,负责通过9090端口监视连接状态,连接有问题时就会自动重连。
  -M 是服务器echo机制使用的端口。

autossh -M 5678 -CqTfnN -D 192.168.0.2:7070 freeoa@remote-host

  参数解释:

-M为autossh参数, -CqTfnN -D 为ssh参数

-M 5678 : 负责通过5678端口监视连接状态,连接有问题时就会自动重连

-C :启动数据压缩传输

-q :安静模式运行,忽略提示和错误

-T :不占用shell

-f :后台运行

-n :配合 -f 参数使用

-N :不执行远程命令,专为端口转发度身打造

-D 192.168.0.2:7070 :指定一个本地机器 “动态的“ 应用程序端口转发,如果不加IP地址,默认只监听127.0.0.1

使用场景:

场景:A是内网服务器192.168.1.10(内部多次nat,无法使用公网IP),B是内网用户转发代理服务器192.168.1.12,C是公网服务器x.x.x.x,D是用户PC

需求:D访问A内网服务器web 8080

方法:
1.B安装autossh

      2.B执行autossh -M 9091 -fCNR 18080:192.168.1.10:8080 root@x.x.x.x

      3.设置B可以无密码登陆C

      4.D上打开putty,设置正向代理,点击Add,save保存

    5.D上打开浏览器:http://127.0.0.1:18080 ,访问到内网A服务器web应用

补充:

注意几个参数:

AUTOSSH_MAXLIFETIME:
Sets the maximum number of seconds the process should live for before killing off the ssh child and exiting.
AUTOSSH_POLL:
poll time in seconds; default is 600.
在实际应用中,发现每隔10分钟,会断线重连,这对应用造成很大影响。

 使用AUTOSSH_MAXLIFETIME参数设定转发存活时间,在这个时间内不会断,但是时间到后,会kill掉进程,无法重连。

 使用AUTOSSH_POLL,可以指定多长时间断线重连,官网原话,实际上没看懂,希望理解的同道指点。

AUTOSSH_POLL	  - poll time in seconds; default is 600.
			    Changing this will also change the first
		    poll time, unless AUTOSSH_FIRST_POLL is
		    used to set it to something different.
		    If the poll time is less than twice the 
		    network timeouts (default 15 seconds) the 
		    network timeouts will be adjusted downward 
		    to 1/2 the poll time.
 在实际中,使用这个参数,依然无法解决我的问题,authossh实际上是调用ssh,可以再加上 -o TCPKeepAlive=yes -o ServerAliveInterval=30 ,解决频繁断线问题。

 一个例子:

 autossh -M 9098 -CNR  x.x.x.x:443:10.x.x.x:443 root@x.x.x.x -f AUTOSSH_POLL  86400 -o TCPKeepAlive=yes -o ServerAliveInterval=30