#!/bin/bash
start(){
printf 'Real IP: %s\n' "$(curl -s ifconfig.co)"
set -- /etc/openvpn/ovpn_tcp/*.nordvpn.com.tcp.ovpn
shift $(( RANDOM % $# ))
screen -S vpn -dm openvpn "$1" # connect
sleep 5 # wait for connection
printf 'VPN IP: %s\n' "$(curl -s ifconfig.co)"
}
stop(){
screen -S vpn -X quit
pkill -f ovpn
}
status(){
printf 'Current IP: %s\n' "$(curl -s ifconfig.co)"
}
case "$1" in
start)
if screen -ls | grep -w vpn &> /dev/null; then
echo "VPN Already Connected";
status
else
start
fi
;;
stop)
stop
;;
*)
status
;;
esac
The previous bashscript give the following answer in case if vpn stop typed.
run of vpn stop if there's active vpn connection:
[root@A ~]# vpn stop
Terminated
run of vpn stop if there's not active vpn connection:
[root@A ~]# vpn stop
No screen session found.
Terminated
What I'm looking to achieve is:
run of vpn stopif there's active vpn connection:
[root@A ~]# vpn stop
VPN Connection Closed Successfully
run of vpn stop if there's not active vpn connection:
[root@A ~]# vpn stop
There's No Active VPN Connection to stop it.