Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/Unix check if VPN connection is Active/Up

I have a code which detects if OpenVPN connection is up or down:

if echo 'ifconfig tun0' | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
then
echo "VPN up"
else
echo "VPN down"
fi
exit 0

now I'm trying to re-write the code to work with PPTP or IPSEC connection. I've tried to do:

if echo 'ifconfig ppp0' | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"

or the same with ipsec but does not work. Is there any other way to detect PPTP or IPSEC connection?

like image 837
Spyder Avatar asked May 05 '15 11:05

Spyder


1 Answers

That echo statement is erroneous. As @unwind says, the single quotes (') should be backtics (`). Your current code is sending the literal value ifconfig ppp0 to grep, which doesn't do anything useful.

But you don't actually need the backtics, either. You can just send the output of ifconfig to grep directory; using echo doesn't get you anything:

if ifconfig ppp0 | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"; then
  echo ppp connection is up
fi
like image 109
larsks Avatar answered Sep 16 '22 16:09

larsks