tags:

views:

327

answers:

1

So at work i use a vpn connection and after that i have to manually set my route table so everything works, I'm trying to write a bash script which will easily distinguish between my home connection and my work connection and set the correct gateway:

#!/bin/bash

# Does the ppp0 interface exist?
cat /proc/net/dev | grep ppp0 > /dev/null
ppp_check=$?

if [ $ppp_check -ne 0 ]; then
        # I'm at home and you should set the ppp0 gateway
        ip r c default via $(ifconfig ppp0 | awk -F "P-t-P:" '{print $2}' | awk -F " " '{print $1}' | tr "\n" " " | awk '{$1=$1};1')
else
        # I'm at work so you should set my work gateway
        ip r c default via 1.1.1.1
fi

ip r a 2.2.2.2/24 dev tun0
.
.
.
ip r a 10.10.10.10/24 dev tun0

The problem is that the script always executes the code for setting the home gateway even though I'm at work and the first IF statement should be skipped.

Any ideas?

A: 

grep will return 0 if it matches the pattern, so you need to test for $ppp-check -eq 0.

You can simplify your test a little bit:

if grep -q ppp0 /proc/net/dev ; then
    # I'm at home
else
    # I'm at work
fi

"grep -q" means you don't need to redirect the output.

camh
works like a charm :) thanks for the help!
f10bit