• SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

static route

eliotte

Occasional Visitor
Hi,

I'm looking where the config file for static route is located.

On my network, I have 2 other router as vpn. One has a US IP, and the other one Germany IP.

I use dnsmasq and force some domains to get sepcific IP and then, use static routing for sending these traffic to my vpn box.

Probleme is, sometimes, the IP I use for some domains change. So I created a bash script to automaticly do a nslookup and now I want to update automatically my dnsmasq and static routing.

Thanks for your help
 
So I found my information by myself. File is located here: /proc/net/route. But I found I don't need it.

What I will try to do is to run my script doing nslookup for all my domains and then run, for each domain:

Code:
ip route delete old_ip
ip route add new_ip via VPN_LOCAL_IP metric 16

I wil add a cron jod running it each hour so it will be always up to date. I will post all my code when it will be finish.
 
Last edited:
First, in the same directory of the script following, you need a file called urls.txt as follow:

Code:
#gamecenter|10.222.215.80
gamecenter.nhl.com
#hulu|10.222.215.245
t2.hulu.com
p.hulu.com
play.hulu.com
data.flurry.com

This is an exemple for some domains. The ip after | is the getway used for this services. In this exemple, I will route all gamecenter traffic by 10.222.215.80 and hulu by 10.222.215.245

The script use jffs partition and create or add stuf to your dnsmasq.conf.add and /etc/dnsmasq.conf

NOTE: bash is really not a language I use often. Here the code:

Code:
#!/bin/sh
#config
dns = "8.8.8.8"

#function

rewrite(){ 
echo > $1".temp";
wasnotwritten=true;
for i in `cat $1`; 
do
        if [[ ${i:0:8} != '#dynamic' ]];then
               	echo $i >> $1".temp"

        else
                echo '#dynamic' >> $1".temp"
                cat ips.txt >> $1".temp"
                wasnotwritten=false;
		break;
        fi
done

if $wasnotwritten ; then
	echo '#dynamic' >> $1".temp"
        cat ips.txt >> $1".temp"
fi

rm $1;
mv $1".temp" $1
}

#retrive Old IP to remove from static routes and Delete it
for i in `cat ips.txt`; 
do
        if [[ ${i:0:1} != '#' ]];then
                ips=$(echo $i | tail -n 1 | cut -d "/" -f 3)
		echo "delete "$i
		ip route delete $ips

        else
                currentgw=$(echo $i | cut -d "|" -f 2);
                currentservice=$(echo $i | cut -d "|" -f 1);
                echo $currentservice >> ips.txt
        fi

done


#NSLOOKUP and add new ips to static route
echo > ips.txt
currentgw = ""
for i in `cat urls.txt`; 
do
	if [[ ${i:0:1} != '#' ]];then
		ips=$(nslookup $i $dns 2>/dev/null | grep Address | tail -n 1 | cut -d " " -f 3)
		echo "address=/"$i"/"$ips >> ips.txt
		echo "add "$i" $ips";
		ip route add $ips via $currentgw metric 16

	else
		currentgw=$(echo $i | cut -d "|" -f 2);
		currentservice=$(echo $i | cut -d "|" -f 1);
		echo $currentservice >> ips.txt
	fi

done

#rewrite Config Files
echo "rewrite config files"
rewrite "/jffs/configs/dnsmasq.conf.add"
rewrite "/etc/dnsmasq.conf"

#relaod dnsmasq
echo "reload DNSMasq"
killall dnsmasq
dnsmasq -c 1500 --log-async -n

echo "done!"

Using a cron job, this can update IPs each hours, day, etc..
 
Last edited:
Simple note, the script must be run in the current directory as:

Code:
 ./static_route

If you try to run in a different path like " /path/.../static_route" it will not work.

You can add this line of code at the beginning to fix it:

Code:
cd /directory/where/script/is/located
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top