charles brosseau
Cadet
- Joined
- Dec 29, 2015
- Messages
- 1
So I've been working on a script to check whether my VPN working (The VPN is only running in one Jail).
What the script does right now is get the public IP of my server (boxIP) and the public IP of the jail running the VPN(jailIP). It then checks to make sure that the Jail's IP is a valid one. If it isn't it will first try to restart the VPN, and then if it still does not work it will shutdown the jail using the API.
Then it will compare the boxIP to the jailIP to make sure they are not the same. If they are it will restart the VPN, then if its still the same, shutdown the jail.
The script will also send me an email if the jail is shutdown like this I know i have to go fix it.
So my issue is that sometimes the public IP of the jail does not work and ends up shutting down the jail even if it is working.
Here is my script, if you guys want to use it for your box feel free. And if you have any feed back feel free to improve it. (I'm not the best in scripting but got it to work, somewhat)
What the script does right now is get the public IP of my server (boxIP) and the public IP of the jail running the VPN(jailIP). It then checks to make sure that the Jail's IP is a valid one. If it isn't it will first try to restart the VPN, and then if it still does not work it will shutdown the jail using the API.
Then it will compare the boxIP to the jailIP to make sure they are not the same. If they are it will restart the VPN, then if its still the same, shutdown the jail.
The script will also send me an email if the jail is shutdown like this I know i have to go fix it.
So my issue is that sometimes the public IP of the jail does not work and ends up shutting down the jail even if it is working.
Here is my script, if you guys want to use it for your box feel free. And if you have any feed back feel free to improve it. (I'm not the best in scripting but got it to work, somewhat)
Code:
#!/bin/bash #Bash script to check the external IP of a jail running a torrent cient #If the jail's ip (jailIP) is invalid or is the same as the main server's IP (boxIP) # the script will first restart the VPN service and if this does not fix the issue, # it will shutdown the jail safely (Using the json API) and send an email to the # provided email containing the last 30 lines of the log file created by this script log=/var/log/Torrent_script.log echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Getting JailIP" >> $log jailIP=$(jexec -n Torrent curl ipecho.net/plain) echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Getting boxIP" >> $log boxIP=$(curl ipecho.net/plain) #check if jail IP is a valid IP if [[ $jailIP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then #valid IP echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Jail has valid external address $jailIP" >> $log else #invalid IP restart vpn and email echo "[$(date +"%Y.%m.%d %H:%M:%S")] ERROR(VPN Failed): Jail IP: $jailIP" >> $log echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Restarting VPN" >> $log jexec -n Torrent service openvpn restart >> $log sleep 10 #update jailIP var echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Refreshing Jail IP" >> $log $jailIP=$(jexec -n Torrent curl ipecho.net/plain; echo) #still fail to get IP, shutdown jail and email if [[ $jailIP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then #valid IP echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Jail has valid external address $jailIP" >> $log else #writing to log and stopping jail echo "[$(date +"%Y.%m.%d %H:%M:%S")] ERROR(VPN Failed to restart): Jail IP: $jailIP" >> $log echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Stopping jail" >> $log curl -v -u root:<passwd> -X POST http://localhost/api/v1.0/jails/jails/2/stop/ --header "Content-Type:application/json" >> $log tail -n 30 $log | mail -s "VPN Failed to Restart - Stopping Jail" <email>@gmail.com fi fi #compare to boxIP same = email both ips and the restart output if [ "$jailIP" == "$boxIP" ]; then #restart VPN and email echo "[$(date +"%Y.%m.%d %H:%M:%S")] ERROR(sameIP): Local IP: $boxIP - Jail IP: $jailIP" >> $log echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Restarting VPN" >> $log jexec -n Torrent service openvpn restart >> $log sleep 10 #update jailIP var echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Refreshing Jail IP" >> $log $jailIP=$(jexec -n Torrent curl ipecho.net/plain; echo) #still the same kill jail and email if [ "$jailIP" == "$boxIP" ]; then echo "[$(date +"%Y.%m.%d %H:%M:%S")] ERROR(Restarted VPN - Failed to Get new IP): Local IP: $boxIP - Jail IP: $jailIP" >> $log echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: Stopping jail" >> $log curl -v -u root:<passwd> -X POST http://localhost/api/v1.0/jails/jails/2/stop/ --header "Content-Type:application/json" >> $log tail -n 30 $log | mail -s "jailIP=boxIP - Stopping Jail" <email>@gmail.com else echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: jailIP ($jailIP) not equal to boxIP ($boxIP)" >> $log fi else echo "[$(date +"%Y.%m.%d %H:%M:%S")] INFO: jailIP ($jailIP) not equal to boxIP ($boxIP)" >> $log fi exit 0