Skip to content

autossh 不管用怎么办

创建于: 2024-02-17

背景

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

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

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

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

上代码

py
# 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')
sh
# crontab -e
* * * * * /usr/bin/python3 /home/xxx/ssh_recover.py