Question

I have a linux machine with two ethernet ports, eth0 and eth1. One port is connected to a network populated with hardware devices which can be controlled via ethernet. This is my "command network". The other port is connected to a network from which I send certain commands to the aforementioned devices, through a web-app on the linux machine's apache server. That network is the "control network."

What I would like to be able to do is allow for either network's IP address to be changed on command, regardless of which network is plugged into which port.

The idea is that the user would, in the web-app, enter the new IP address for either the command network or the control network and click submit, which would launch an AJAX request to the server. The server would then determine whether the request came from eth0 or eth1, and then either change that port's IP address or the other port's IP address, depending on whether the command or control network was being changed.

Is there any way of determining which ethernet port the request came from in Python?

Was it helpful?

Solution

Is there any way of determining which ethernet port the request came from in Python?

Not readily.

However, what you could do is set up some iptables rules to NAT port 80 on one interface to something like 127.0.0.1:8000 and the other interface to 127.0.0.1:8001.

E.g.:

iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8000
iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8001

Then you could listen on both ports (8000 and 8001) and tell them apart either by python instance (depending on your python web framework) or by destination port.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top