Marek Novotny wrote:
> Committed to v4.5. This will pre-test for openvpn and if it finds it,
> it will kill everything. This script now assumes it is the sole
> connection and status tool. So a double run will close it all up as
> the script assumes something is wrong since it didn't open the
> connection.
>
> If you don't like that idea, all you have to do is remove one call to
> the kill routine. For myself, I don't run it unless I want it to do the
> whole job for me. So if I see openvpn already running, I want the script
> to kill it so that all I have to do is rerun it.
>
>
https://github.com/marek-novotny/vpntools
Got the 4.5 version and will test.
I will need to add a few things though:
ARGUMENTS:
1. Accept zero or one argument
a. The one argument can be any portion of a filename
b. Then it will randomly choose the next ovpn file based on that argument
KICKORKEEP:
2. If the vpn session fails or if I kill the VPN session, allow me to do two things:
a. Allow me to kickorkeep (i.e., move) the file elsewhere with a case statement
b. Allow me to load in the next random file that fit the requirements
I already have this in the old vpnrun script (I posted the snippet that
allows this, a few moments ago) so I would need to port it over.
The use model is this:
A. I go to the vpn_winners directory which might have a thousand config files
B. I either run "vpnstatus" or "vpnstatus US" or "vpnstatus 12.123.123.12"
or "vpnstatus Buffalo" or "vpnstatus <whatever>".
C. It will start vpn on a random file in the selected set
Since the vpngate files are so flaky, the next step is important!
D. If the session fails, it allows me to kickorkeep the bad file into pre-defined bins
E. Likewise if I kill the session myself (e.g., if it's too slow for my liking)
F. And it will just go to the *next* random file in that selected set.
Here's the verbatim vpnrun.sh that currently does all that:
#!/bin/bash
# vpnrun runs vpn on all *.ovpn files or just selected files
#########################################################
# script: vpnrun.sh was initially based off vpntest by Marek Novotny
# version: original version of vpntest was .01
# date: original date of vpntest was 2015-11-24
# purpose: run vpn but kick out bad files
#########################################################
# To Use: vpnrun (this will select files *.ovpn)
# Or Use: vpnrun US (this will select files *US*)
# Or Use: vpnrun Buffalo (this will select files *Buffalo*)
# Or Use: vpnrun whatever (this will select files *whatever*)
set -u
# Abort if openvpn is already running
openvpnPID=$(pgrep openvpn)
if [ $? -ne 1 ] ; then
echo "Oops: It seems openvpn is already running; aborting"
exit 1
fi
# Creates a one-line log file containing the filespec to the current ovpn file
_vpnPIDtmpFile=/tmp/openvpn.log
# Define good directories inside the current directory
# In general, run vpngeo & vpnspeed on the good ones:
LOCAL_GOOD_DIR=./good
# If they're really really fast, make note of them here:
LOCAL_FAST_DIR=./fast
# If they suck at nntp, then push them into here:
_badNntpLocalhost=./bad_nntp
# u (the most common of errors)
# Change the IP address to a bogus one in the config file & you get this error.
# UDPv4 link local: [undef]
# UDPv4 link remote: [AF_INET]
1.2.3.4:1234
_badUdpLink=$HOME/doc/cert/bad_udp
# a (the second most common of errors)
# Socket Buffers: R=[87380->131072] S=[16384->131072]
# Attempting to establish TCP connection with [AF_INET]
1.2.3.4:1234 [nonblock]
_badAttemptDir=$HOME/doc/cert/bad_attempt
# c (happens if you wait long enough after some attempts)
# Change the hostname to
www.google.com and you get this.
# Attempting to establish TCP connection with [AF_INET]
1.2.3.4:1234 [nonblock]
# TCP: connect to [AF_INET]
1.2.3.4:1234 failed, will try again in 5 seconds: No route to host
_badTcpConnect=$HOME/doc/cert/bad_tcpconnect
# l (very rare TLS initial packet, happens after UDP)
# UDPv4 link local: [undef]
# UDPv4 link remote: [AF_INET]
147.47.50.52:1195
# TLS: Initial packet from [AF_INET]
147.47.50.52:1195, sid=43600901 812e7825
_badTlsInitialPacket=$HOME/doc/cert/bad_tlsinitialpacket
# r (happens if you get a tls error)
# TLS Error: TLS object -> incoming plaintext read error
# TLS Error: TLS handshake failed
# SIGUSR1[soft,tls-error] received, process restarting
# Restart pause, 2 second(s)
_badRestartPause=$HOME/doc/cert/bad_restart
# s
# SENT CONTROL [
s4fpa9jg1yo9tbhk.org]: 'PUSH_REQUEST' (status=1)
# AUTH: Received control message: AUTH_FAILED
# SIGTERM[soft,auth-failure] received, process exiting
_badSigterm=$HOME/doc/cert/bad_sigterm
# d (this is when there are HTML files posing as ovpn files)
# Options error: Unrecognized option or missing parameter(s) in file.ovpn:2: <!DOCTYPE (2.3.2)
_badDoctype=$HOME/doc/cert/bad_doctype
# p (this is a rare push-request failure)
# SENT CONTROL [
8ttpjpvu35f6656.jp]: 'PUSH_REQUEST' (status=1)
_badPushRequest=$HOME/doc/cert/bad_pushrequest
kickOrKeep () {
echo ""
echo "$openvpnFileName"
echo ""
echo "WORKING: (good/fast/nntp)"
echo "RARE: (doctype/sigterm/restart)"
echo "ATTEMPT->(tcp)"
echo "UDP ->(tls)"
echo "COMMON: (attempt/udp)"
echo ""
echo "(good,fast,nntp) (pushreq doctype sigterm restart) (attempt-TcP udp-TlS) "
read -p "(g f n ) (p d s r ) (a -> c u -> l ) " answer
case $answer in
[Yy]* )
echo "do nothing..."
;;
[Gg]* )
mkdir -p $LOCAL_GOOD_DIR
echo "mv -i \"$openvpnFileName\" $LOCAL_GOOD_DIR..."
# mv -i "$openvpnFileName" $LOCAL_GOOD_DIR
mv "$openvpnFileName" $LOCAL_GOOD_DIR
;;
[Ff]* )
mkdir -p $LOCAL_FAST_DIR
echo "mv -i \"$openvpnFileName\" $LOCAL_FAST_DIR..."
# mv -i "$openvpnFileName" $LOCAL_FAST_DIR
mv "$openvpnFileName" $LOCAL_FAST_DIR
;;
[Ss]* )
mkdir -p $_badSigterm
echo "mv -i \"$openvpnFileName\" $_badSigterm..."
# mv -i "$openvpnFileName" $_badSigterm
mv "$openvpnFileName" $_badSigterm
;;
[Nn]* )
mkdir -p $_badNntpLocalhost
echo "mv -i \"$openvpnFileName\" $_badNntpLocalhost..."
# mv -i "$openvpnFileName" $_badNntpLocalhost
mv "$openvpnFileName" $_badNntpLocalhost
;;
[Rr]* )
mkdir -p $_badRestartPause
echo "mv -i \"$openvpnFileName\" $_badRestartPause..."
# mv -i "$openvpnFileName" $_badRestartPause
mv "$openvpnFileName" $_badRestartPause
;;
[Uu]* )
mkdir -p $_badUdpLink
echo "mv -i \"$openvpnFileName\" $_badUdpLink..."
# mv -i "$openvpnFileName" $_badUdpLink
mv "$openvpnFileName" $_badUdpLink
;;
[Aa]* )
mkdir -p $_badAttemptDir
echo "mv -i \"$openvpnFileName\" $_badAttemptDir..."
# mv -i "$openvpnFileName" $_badAttemptDir
mv "$openvpnFileName" $_badAttemptDir
;;
[Cc]* )
mkdir -p $_badTcpConnect
echo "mv -i \"$openvpnFileName\" $_badTcpConnect..."
# mv -i "$openvpnFileName" $_badTcpConnect
mv "$openvpnFileName" $_badTcpConnect
;;
[Ll]* )
mkdir -p $_badTlsInitialPacket
echo "mv -i \"$openvpnFileName\" $_badTlsInitialPacket..."
# mv -i "$openvpnFileName" $_badTlsInitialPacket
mv "$openvpnFileName" $_badTlsInitialPacket
;;
[Dd]* )
mkdir -p $_badDoctype
echo "mv -i \"$openvpnFileName\" $_badDoctype..."
# mv -i "$openvpnFileName" $_badDoctype
mv "$openvpnFileName" $_badDoctype
;;
[Pp]* )
mkdir -p $_badPushRequest
echo "mv -i \"$openvpnFileName\" $_badPushRequest..."
# mv -i "$openvpnFileName" "$_badPushRequest"
mv "$openvpnFileName" "$_badPushRequest"
;;
* )
kickOrKeep
;;
esac
}
# If no arguments are provided, then use *.ovpn* as the argument for the next random file.
# If an argument is provided (e.g., US), then use "*that*" as the argument for the next file in order.
if [ $# -eq 0 ]; then
lsFileName=\*.ovpn
else
lsFileName=\*${1}\*
fi
# Use shuffle to randomly choose the next ovpn file:
for openvpnFileName in $(ls $lsFileName | shuf ) ; do
echo "The current \$openvpnFileName is $openvpnFileName"
echo "sudo openvpn --config \"$openvpnFileName\" --script-security 2 --up /etc/openvpn/update-resolv-conf --down /etc/openvpn/update-resolv-conf ..."
DATE=$(date)
echo $(pwd)"/"$openvpnFileName > $_vpnPIDtmpFile
sudo openvpn --config "$openvpnFileName" --script-security 2 --up /etc/openvpn/update-resolv-conf --down /etc/openvpn/update-resolv-conf
kickOrKeep
done
#END SCRIPT#