使用脚本建立 crontab 反向 SSH 连接

Den*_*nis 6 ssh cron autossh

我在 NAT 后面有一台 PC,它与我的 Digitalocean VPC 建立反向 SSH 连接。我利用这个反向 SSH 连接从家里登录到我的办公室 PC(我被授权这样做)并复制文件和做其他重要的事情。

虽然不常见,但我注意到我的办公室 PC 重新启动(由于电源故障等)并中断了它与我的 VPC 建立的反向 SSH 连接。在这种情况下,我无法从我的家用 PC 连接到我的办公室 PC。

我运行以下脚本,使反向连接 + 动态代理匿名我在办公室 PC 上生成的流量(因为我不需要共享浏览信息)。

autossh -CD 8080 -i digitalOcean -R 8081:localhost:22 root@IPofDigitalOceanPC
Run Code Online (Sandbox Code Playgroud)

重新启动后,我无法在办公室 PC 上再次运行此脚本,因为我不在那里。为了解决这个问题,我安装了以下 crontab。

注意:rev.sh文件包含上述行。证书“digitalOcean”和 rev.sh 位于Ubuntu home. 因此,当我./rev.sh在我的 Ubuntu 终端中执行时,我获得了一个动态代理并访问了我的 DigitalOcean 服务器。这种方法100%有效。

但是,当我使用以下方法安装 crontab 时,我的 ubuntu PC 永远不会创建动态代理。我可以看到这一点,因为当我从谷歌浏览器检查这个代理时,它说代理拒绝连接。

这是我作为 root 的 cronjobs 尝试的 cronjobs。我也以普通用户的身份尝试过这些,但它们仍然不起作用。

@reboot bash /home/user/rev.sh 
@reboot /home/user/rev.sh 
@reboot cd /home/user && ./rev.sh
Run Code Online (Sandbox Code Playgroud)

然后我在当前时间前几分钟安装了一个 crontab 并等待它执行。

24 12 * * * bash /home/user/rev.sh
24 12 * * * /home/user/rev.sh
24 12 * * * reboot 
Run Code Online (Sandbox Code Playgroud)

这些也没有执行。

我也试过48 15 * * * bash /home/user/rev.sh >> test3*/1 * * * * reboot -f >> test但 test3 和 test 什么都没有。但是文件已经创建了!!通过 crontab !

请帮助我发现我的错误。这个网站上有很多关于我的问题的类似问题。因此,我已经提到了很多答案,但似乎没有一个有帮助。

Sam*_*ske 1

更好的解决方案是使用看门狗。watchdog 是一个守护进程,它将监视正在运行的进程,如果它们退出,它将自动重新启动它们。

在 Ubuntu 16.04 上安装看门狗

sudo apt-get install watchdog
Run Code Online (Sandbox Code Playgroud)

看门狗如何工作

守护进程将使用参数或watchdog(8)执行脚本。(参见手册页部分)。当您的看门狗脚本检查进程是否正在运行并采取操作来修复它时,它会处理这两个参数。/etc/watchdog.dtestrepairTEST DIRECTORYwatchdog(8)

您可以通过修改来配置看门狗/etc/watchdog.conf(请参见watchdog.conf(5))。

示例watchdog.d脚本

举个例子/etc/watchdog.d/autossh_script(它有755权限并且由 拥有root)。

注意:您可能需要$targetuser在示例脚本中自定义环境变量。 sam是我的用户名。

#!/bin/bash

targetuser=sam

runTest=false
runRepair=false

case $1 in
  test)
    runTest=true
  ;;
  repair)
    runRepair=true
    repairExitCode=$2
  ;;
  *)
    echo 'Error: script needs to be run by watchdog' 1>&2
    exit 1
  ;;
esac

if ${runTest}; then
  #run a test here which will tell the status of your process
  #the exit code of this script will be the repairExitCode if it is non-zero
  if ! pgrep autossh &> /dev/null; then
    #autossh not running; notify watchdog to repair
    exit 1
  else
    #autossh running; no action necessary
    exit 0
  fi
fi

if ${runRepair}; then
  #take an action to repair the affected item
  #use a case statement on $repairExitCode to handle different failure cases
  su - ${targetuser} -c 'nohup autossh -f -- -NCD 8080 -i digitalOcean -R 8081:localhost:22 root@IPofDigitalOceanPC'
  exit 0
fi
Run Code Online (Sandbox Code Playgroud)
  • 我在示例脚本中添加了-N您的ssh命令,因此它仅启动隧道,但不会尝试创建登录 shell。
  • 我添加-fautossh使其在后台运行。
  • 示例脚本使用简单的pgrep模式。但是,您可以进一步将测试范围缩小到特定用户,甚至可以使用该过程的模式。请参阅pgrep(1)如何使用 pgrep 进一步自定义测试。

配置看门狗

/etc/watchdog.conf/etc/defaults/watchdog是配置看门狗的地方。看watchdog.conf(5)

需要注意的一件事是用户脚本默认每秒执行一次。我建议将其增加到至少 30 秒,除非您需要更多的实时检查。调整interval中的设置watchdog.conf

故障排除

您可能需要/etc/watchdog.d在脚本之前创建目录。

/var/log/watchdog/*包含与看门狗相关的日志和错误。如果您的脚本输出到 stdout 或 stderr,那么它将被写入那里。在我的系统上,我注意到我的脚本大约每秒执行test一次repair。如果您在脚本中使用 echo,那么它应该只是临时的,仅用于调试目的。否则,除非出现错误,否则建议丢弃输出。

如果您的脚本根本没有运行,请检查权限:ls -l /etc/watchdog.d