How do I route traffic through specific interface for a specific process

I have a script that runs from cron every 15 minutes. The script is meant to ping my DNS server to update a dynamically changing IP. I want this script to ping through the wlan interface and not the eth0.

However, other processes should use eth0 as the primary interface to access the internet.

How do I do this for a specific process only without disturbing other processes? Is there a way to direct traffic through interface like directing stdout? like ls -al > /dev/tty2'. Can chroot help me in this by giving me a sandboxed environment?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The solution is to set the Netfilter packet mark which can be used by advanced routing. There is no way to match by process id, but Netfilter allows to match on process UID or GID. In this case it seems to be the easiest solution to create a new system user especially for this task.

iptables -t mangle -A OUTPUT -m owner --uid-owner wlanping -j MARK --set-mark 42

Create a new routing table (forecewlan) in /etc/iproute2/rt_tables and chose the wlan gateway as default gateway:

ip route add default via $WLAN_GATEWAY

and force the use of this routing table for packets with this mark:

ip rule add fwmark 42 table forcewlan

Maybe you also have to copy this kind of entry from the main routing table:

192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.100


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x