feling.net/_posts/2024-02-17-autossh.md

1011 B

layout title categories tags lastModifiedDate
pages autossh 不管用怎么办
配置
ssh
autossh
keepalive
2024-02-17

背景

家里的个人服务器, 需要通过 ssh 连接到香港的云服务器. 但是网络连接不稳定, 经常断连.

尝试过 autossh, 但是用不明白, 没啥效果.

每次使用, 还是需要在服务器上手动连接. 就想着有没有啥迂回的办法, 把这个麻烦解决掉.

写个脚本定时检查吧. n 年前写过的 python 获取当前网速 的笔记可以直接拿来抄.

上代码

# ssh_recover.py

import os

with os.popen('ps x | grep ssh') as ps:
    ps_response = ps.read()
    if '1.1.1.1' not in ps_response:
        os.system('ssh root@1.1.1.1 -fgnNC -D7890')
    if 'xxx.com' not in ps_response:
        os.system('ssh root@xxx.com -fgnNC -R1604:127.0.0.1:80')
# crontab -e
* * * * * /usr/bin/python3 /home/xxx/ssh_recover.py
  • 文章目录 {:toc}