Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
clewsy
scripts
Commits
0a1cd352
Commit
0a1cd352
authored
Mar 23, 2020
by
clewsy
Browse files
Typos, formatting.
parent
8dbed819
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
16 deletions
+16
-16
vpn.sh
vpn.sh
+16
-16
No files found.
vpn.sh
View file @
0a1cd352
...
...
@@ -21,7 +21,7 @@ OPENVPN_FAIL=5
DEFAULT_CONF
=
"/home/jc/openvpn/Windscribe-Australia_UDP.ovpn"
#Define a temp file into which ipinfo.io data will be entered and then parsed
TEMP_FILE
=
$(
dirname
"
$0
"
)
/t
e
mp
#using $dirname of $0 will create temp in the current working directory
TEMP_FILE
=
/tmp
/vpn_tempfile
USAGE
=
"
Usage:
$(
basename
"
${
0
}
"
)
<option> [openvpn config file]
...
...
@@ -40,8 +40,7 @@ while getopts 'ckh' OPTION; do ## Call getopts to identify selected options an
h
)
echo
-e
"
${
USAGE
}
"
## -h option just prints the usage then quits.
exit
${
SUCCESS
}
## Exit successfully.
;;
?
)
echo
-e
"Invalid option/s."
?
)
echo
-e
"Invalid option/s."
echo
-e
"
$USAGE
"
## Invalid option, show usage.
exit
${
BAD_USAGE
}
## Exit.
;;
...
...
@@ -56,11 +55,12 @@ if [ $# -gt 1 ]; then ## Check if more than one argument was entered.
exit
${
BAD_USAGE
}
fi
##########Define openvpn config fil
r
.
##########Define openvpn config fil
e
.
VPN_FILE
=
${
1
-
"
${
DEFAULT_CONF
}
"
}
## First argument is the openvpn config file.
## If argument not provided, set default (defined at top of script).
## Syntax: parameter=${parameter-default}
##########Validate config file.
##########Validate openvpn config file.
if
[
!
-f
"
${
VPN_FILE
}
"
]
;
then
## If file is not a regular file or is missing.
echo
-e
"
\n
${
RED
}
Error
${
RESET
}
: Invalid openvpn config file. Quitting."
echo
-e
"
${
USAGE
}
"
...
...
@@ -84,12 +84,12 @@ if ! curl --silent --connect-timeout 5 --max-time 10 --output "$TEMP_FILE" ipinf
fi
##########Parse specific details from the temp file.
current_ip
=
$(
grep
-m
1
"ip"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
current_city
=
$(
grep
"city"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
CURRENT_IP
=
$(
grep
-m
1
"ip"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
CURRENT_CITY
=
$(
grep
"city"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
##########Print out the current ip and city (without vpn).
echo
-e
"
\n
Current ip:
$
current_ip
"
echo
-e
"Current city:
$
current_city
"
echo
-e
"
\n
Current ip:
$
{
CURRENT_IP
}
"
echo
-e
"Current city:
$
{
CURRENT_CITY
}
"
##########Connect to the vpn.
echo
-e
"
\n
Running openvpn using config file at
\"
$VPN_FILE
\"
"
...
...
@@ -101,24 +101,24 @@ if ! sudo openvpn --config "${VPN_FILE}" --daemon ; then ## Execute openvpn then
fi
##########Loop until the vpn is active - determined by a change in the ip.
new_ip
=
${
current_ip
}
while
[
"
${
new_ip
}
"
==
"
${
current_ip
}
"
]
NEW_IP
=
${
CURRENT_IP
}
while
[
"
${
NEW_IP
}
"
==
"
${
CURRENT_IP
}
"
]
do
if
!
curl
--silent
--connect-timeout
5
--max-time
10
--output
"
$TEMP_FILE
"
ipinfo.io
;
then
#Execute curl command but exit if it fails
rm
"
$TEMP_FILE
"
echo
-e
"
\n
${
RED
}
Error
${
RESET
}
: Failed to pull data from
\"
ipinfo.io
\"
. Quitting..."
exit
${
NO_IPINFO
}
fi
new_ip
=
$(
grep
-m
1
"ip"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
NEW_IP
=
$(
grep
-m
1
"ip"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
done
new_city
=
$(
grep
"city"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
NEW_CITY
=
$(
grep
"city"
"
$TEMP_FILE
"
|
cut
-d
":"
-f
2 |
cut
-d
"
\"
"
-f
2
)
##########Print out the new apparent ip & city.
echo
-e
"
\n
${
GREEN
}
Connected.
${
RESET
}
"
echo
-e
"New ip:
$
new_ip
"
echo
-e
"New city:
$
new_city
\n
"
echo
-e
"New ip:
$
{
NEW_IP
}
"
echo
-e
"New city:
$
{
NEW_CITY
}
\n
"
##########Delete the temp file.
rm
"
$TEMP_FILE
"
exit
${
SUCCESS
}
\ No newline at end of file
exit
${
SUCCESS
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment