Ping google.com to see if vpn-server is up, if down, reconnect to another one - bash

I am using a VPN server on a vps. The VPN service I use can of course go down for a few minutes or maybe even hours by accident or whatever. If it happens, I would like to quickly reconnect to another vpn-server with the command "vpnapp reconnect".
Does anyone know how to do a bash script I will run every minute via cronjob that:
Pings google.com
2A) If it can be reached, do nothing
2B) If it cannot be reached - which would mean the vpn server is down/not working - enter terminal command "vpnapp reconnect" which will make the vpn application reconnect to next available server they offer.
Would be so thankful if anyone could help me. I have ZERO programming knowledge. To me this is pure science.
Thanks in advance.
Oski

Can it be this simple?
if ping -c 4 google.com ; then exit ; else vpnapp reconnect ; fi
So if 4 packages are received, meaning google.com can be reached it will just exit the bash script.
if not, then it will just enter
vpnapp reconnect
as a command in the terminal... reconnecting the vpn.
or does it need to be "vpnapp reconnect" embedded in "?
Lol i am so weak at this. I really dont trust my own suggestion anyway so would love to hear a real programmer speak. Thanks!!

You don't need a bash script, you can just add this cron job (with crontab -e):
* * * * * ping -c2 google.com || vpnapp reconnect
* * * * * Run the command every minute.
ping -c2 google.com Send 2 pings to google.com.
|| vpnapp reconnect mean that if ping failled it will run vpnapp reconnect

Related

Make bash wait until remote server kickstart is done (it will create a file when it's done)

I am creating a script to kickstart several servers. I am nearly finished, however I want the bash script to wait until the server kickstart is done.
When the kickstart is done and the server is rebooted a file will be created on the remote kickstarted server which is located under "/root/" and is called "kickstart-DONE"
Is it possible to make the bash script wait until it sees this file and then post something like "Done!"...?
I tried searching the forums and internet, but probably I am searching incorrectly, as I was unable to find something relevant to this issue. Heck, I don't even know if this is possible at all.
So in short; I run my script which kickstarts a server. After the kickstart is done it will create a file on the remote (- kickstarted) server called: kickstart-DONE. This would be an indication for the script that the kickstart is fully done and the server can be used. How do I make the script aware of this?
I hope someone understands what I mean and trying to achieve....
Thanks in advance.
//EDIT
SOLVED! Thanks to Cole Tierney!
Cole Tierney gave some good answers, however though it works it does not wait until the server is kickstarted. I ran the script to kickstart a server and in the end it was running the provided command:
ssh root#$HWNODEIP "while ! test -e /root/kickstart-DONE; do sleep 3; done; echo KICKSTART IS DONE...\!"
However since the kickstart can take some time (depending on server speed and such; ranging from 15 minutes to 1 hour). The command timed out:
ssh: connect to host 100.125.150.175 port 22: Connection timed out
Is there a way that the script does not time out at all and keeps it alive until the server gets back or until it takes more than 1 hour or so?
Maybe there is also a way to make it show that the script is still active? Like "Waiting... 5 minutes passed." "Waiting... 10 minutes passed." etc.
So it gives the current user some information that it not died?
You could call sleep until the file exists:
while ! test -e /root/kickstart-DONE; do sleep 3; done; echo kickstart done
Or sleep until the server is accepting ssh connections. Run the following netcat command locally to check when port 22 is open on the server (remove echo closed; if you don't want the extra feedback):
while ! nc -zw2 $HWNODEIP 22; do echo closed; sleep 3; done
On a side note, it's useful to setup a host entry in ~/.ssh/config. You can add all sorts of ssh options here without making your ssh command unwieldy. Options that are common to all host entries can be added outside of the host entries. See man ssh_config for other options. Here's an example (server1 can be anything, replace <server ip> with the server's ip address):
Host server1
Hostname <server ip>
User root
Then to use it:
ssh server1 'some command'
Note that many systems will not allow ssh connections from root for security reasons. You may want to consider adding another user for kickstart stuff. Add this user to sudoers if root access is needed.

How to run bash code inside netcat connection

So i have a problem : i want to run bash commands when i connect to a server. I have to capture the flag when i connect to a server X via port Y, it stays open only 5-10 sec then closes and you need to guess the random number from 1 to 1000. The for is not the problem (for i in {1..1000} echo $i), the problem is how i make the netcat wait until the connection is done and then run a command. I searched on stackoverflow and a lot of websites and it is not what i need.
while ! nc server port; do
sleep 0.1
done
and somwhere here run the command but if you run it over the while, it will run when the netcat is closed

Bash code to check for SSH/Samba connections?

I currently use the following cron job to put the computer in suspend mode and wake it up the following day:
0 13 * * * /usr/sbin/rtcwake -m standby -u -t $(date +\%s -d 'tomorrow 1145')
I'd like to replace this with a call to a Bash script that will
be called every hour…
check if anyone is currently connected through SSH or Samba (backup job)
If no one is connected, go into suspend mode
else try again the next hour.
Does someone have some working code I could use?
Thank you.
w:
Show who is logged on and what they are doing.

Why does my detached loop eventually get killed?

In order to connect my VPS back to my home computer I have this script running on my home computer:
{ while true ; do ssh -nNR 1234:localhost:22 root#12.34.56.78 ; sleep 300 ; done ; } & disown
It starts a reverse ssh tunnel. If the connection gets broken for whatever reason the connection is restarted after 5 minutes. This seemed to be working well at first, but then I noticed that the loop only keeps running for a few days at most.
Why does it stop or get killed?
check how many processes this ssh command have invoked by this loop, you might want to add the following option in your ssh command line:
-o ExitOnForwardFailure=yes
check autossh which is working much nicer. I am using autossh+Cygwin under my home PC and can connect between my office and home for days without any interruption.

Bash script to monitor ISDN connection

On a Ubuntu 10.04 Server I would like to do the following with a bash script:
Create a service that monitors the ISDN connection and if downtime exceeds 60 seconds forces reconnect.
My current solution looks something like this:
#!/usr/bin/bash
LOGFILE=/home/msw/router/ping-stats.txt
TIME="`date +%C%y%m%d%H%M`"
/sbin/ping -c 1 google.com > /dev/null 2>&1
if [ "$?" == "0" ]
then
STATUS=1
else
STATUS=0
fi
echo "$TIME $STATUS" >> $LOGFILE
Since bandwidth is precious on an ISDN connection, I would like to avoid the ping and replace it with a command that simply checks for the status of the network device. Is it possible to infer from that if the connection is "up"?
I would also like to implement the solution as a service that checks connectivity continually instead of checking periodically with a cronjob.
I would really appreciate it if somebody could push me in the right direction.
Thank you
For quick-and-dirty there's nm-tool. dbus-send can be a bit more precise, but needs knowledge of how D-Bus works with NetworkManager. If you want something persistent then you'll need to learn how to interact with D-Bus, but that may require using something a bit lower-level such as Python.
Is your ISDN provided by an internal adapter or via an Ethernet connection? If you have an external "modem" you'd need to query that using SNMP or its proprietary facility.
You can do your test this way, by the way:
if /sbin/ping -c 1 google.com > /dev/null 2>&1
then
...
Also, a single ping is a very small number of bytes. If you're only doing it a few times a minute you may never notice it.

Resources