Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate SNX restart with crontab

Tags:

linux

cron

vpn

I am using VPN connection using SSL Network Extender(SNX) to connect to remote server. The connection from the remote server is limited to only 12 hours. After that the connection is being disconnected and have to restart the SNX server again. To overcome those hardship I am trying to automate SNX restart using crontab.

  1. I have created one shell script file called vpn.sh.
#!/bin/bash
snx -d
sleep 3
echo 'password' | snx
  1. I have config file call .snxrc inside home directory
server server.com
username username
reauth yes
  1. Inside crontab (crontab -e) config I have

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

It runs every 12 hours. But snx -d runs successfully but on reaching echo 'newpass6' | snx I am getting this error:

Failed to init terminal!

Any body encountered such issues? Please help me. I have been struggling for a week now. Thanks in advance.

I have followed this link to setup snx

like image 220
Tashi Dendup Avatar asked Oct 14 '25 18:10

Tashi Dendup


2 Answers

The approved answer doesn't work for me. It creates an empty tmux session with no command executed inside. So this is my way to do this task:

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

Only one command to make it work. nohup is required because snx process is going to the background and returning prompt. After that, tmux exits, and snx is not assigned to the terminal. Without nohup after tmux exits, the system will terminate the snx process.

like image 136
QkiZ Avatar answered Oct 17 '25 10:10

QkiZ


Because snx client cannot start without a terminal. So i put in my script these commands to start snx in a byobu session.

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"
like image 44
ducnc Avatar answered Oct 17 '25 10:10

ducnc