使用 crontab 自动重启 SNX

Automate SNX restart with crontab

我正在使用使用 SSL 网络扩展器 (SNX) 的 VPN 连接来连接到远程服务器。来自远程服务器的连接仅限于 12 小时。之后连接被断开,必须重新启动 SNX 服务器。为了克服这些困难,我正在尝试使用 crontab 自动重启 SNX。

  1. 我创建了一个名为 vpn.sh 的 shell 脚本文件。
#!/bin/bash
snx -d
sleep 3
echo 'password' | snx
  1. 我在主目录中有配置文件调用 .snxrc
server server.com
username username
reauth yes
  1. 我在 crontab (crontab -e) 中配置

* */12 * * * bash /home/username/vpn.sh > /home/username/cron.log

每 12 小时运行一次。但是 snx -d 运行成功,但在到达 echo 'newpass6' | snx 时出现此错误:

Failed to init terminal!

有人遇到过这样的问题吗?请帮我。我已经挣扎了一个星期了。提前致谢。

我已按照此 link 设置 snx

因为没有终端就无法启动snx客户端。所以我在脚本中放入了这些命令以在 byobu 会话中启动 snx。

byobu new-session -d -s vpn;
byobu new-window -t vpn:1 -n "snx" "echo your_password | snx -s your_ip -u your_user; sleep 10"

批准的答案对我不起作用。它创建一个空的 tmux 会话,里面没有执行任何命令。所以这是我完成这项任务的方法:

byobu-tmux new-session -d "echo <password> | nohup snx -s <host> -u <user>"

只需一个命令即可使其工作。 nohup 是必需的,因为 snx 进程将进入后台并返回提示。之后,tmux 退出,snx 没有分配给终端。 tmux退出后没有nohup,系统会终止snx进程