I am running some application in multiple network namespace. And I need to create socket connection to the loopback address + a specific port in each of the name space. Note that the "specific port" is the same across all network namespaces. Is there a way I can create a socket connection like this in python?
Appreciate any pointer!
I just came across this post while looking into network namespaces and using python to interact with them. In regards to your question about non-root users running the setns(), or similar functions, I believe that is achievable. In a small script that creates the red and blue namespaces mentioned in this post, you could also set a linux capability inside of the new namespace that would allow non-root users to attach and bind. Directly from the man page, we see this description: call setns(2) (requires CAP_SYS_ADMIN in the target namespace);
Capabilities can be added to binaries such as the python2.7 or it can also be added to systemd processes. For example, if you look at the default openvpn-server service file on a centos 7 or RHEL 7 you can see added capabilities so that it can run without root privileges: CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN.....
I know this isn't an answer to the original question, but I do not have enough reputation to reply to comments at the moment. I would suggest looking at capabilities and all of the options provided if you are security conscious and looking to run code as non-root users.
Can I open sockets in multiple network namespaces from my Python code?