I needed to connect to a device which was on a wifi network but also needed to stay connected to my local LAN
First easy setup was to add a wifi dongle to my PC and connect to wireless network. This worked fine. I set a static IP with no default gateway and i could connect to the device on the wifi and continue using my wired LAN for internet E.T.C
I could have stopped there, job done.
But as we all like to play a little i thought i would try and do it a “better” way.
Sometimes i wanted to connect to this wifi device from another PC and didn’t want to buy another wifi dongle for each device.
Now you might be thinking instead of this device being left on its own wifi network and me struggle to connect to it why don’t i just change the setup on this device so that it connects to my existing wifi?
Unfortunately this device can not be configured to do that. Why? i don’t know either. Its hard coded to be on its own wifi and cant join any wifi.
The solution? Route between the wifi network and my wired network with a router.
As i have some Raspberry Pi’s doing other tasks already (TvHead and email server) i thought i would add this feature to one of them.
As the Raspberry Pi 3B has built in wifi and wired network it made sense to use that device.
Same as pretty much everyone else a started by googling it. The Raspberry PI has a great community!
Long story short i found these posts the most helpful.
http://qcktech.blogspot.com/2012/08/raspberry-pi-as-router.html
Now i didn’t use everything that was in each post so i will copy below the bits i did use
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.0.0/24 -o eth0 -j SNAT --to-source 192.168.0.4
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 192.168.1.0/24 -o wlan1 -j SNAT --to-source 192.168.1.4
echo 1 > /proc/sys/net/ipv4/ip_forward
nano /etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
Then on each PC i added a static route ( if i had a better internet router on my LAN i’m sure i could add this static route there so any device on my network would be able to reach this device/wifi network)
Open a cmd window with elevated rights (run as administrator)
route add (destination ip) MASK (network mask of destination network) (gateway ip) (-p) (metric)
route add 192.168.1.0 MASK 255.255.255.0 192.168.0.4 -p
-p makes it persistent so it stays after reboots.
Not sure about metric. think its to do with the amount of hops through the network. It can be ignored for this project i believe.
Works a treat for me. Hope it works for you also