Python Port Forwarding / Multiplexing Server

I would like to make a server that listens on UDP port 162 (SNMP trap) and then redirects this traffic to multiple clients. It is also important that the source port and address remain the same (address merging).

I think that the best tool for this would be Twisted or Scapy, or perhaps vanilla sockets, only I can not find anything in the documentation for Twisted about sending or swapping the source address.

Any solution for this?

Edit: added bounty, mybe any solution with iptables?

+7
python networking iptables
source share
2 answers

I'm not comfortable with twisting or scapy, but it's pretty simple to do this with vanilla python sockets. An added benefit of this is that it will be even more portable. This code works in my limited tests:

#!/usr/bin/python from socket import * bufsize = 1024 # Modify to suit your needs targetHost = "somehost.yourdomain.com" listenPort = 1123 def forward(data, port): print "Forwarding: '%s' from port %s" % (data, port) sock = socket(AF_INET, SOCK_DGRAM) sock.bind(("localhost", port)) # Bind to the port data came in on sock.sendto(data, (targetHost, listenPort)) def listen(host, port): listenSocket = socket(AF_INET, SOCK_DGRAM) listenSocket.bind((host, port)) while True: data, addr = listenSocket.recvfrom(bufsize) forward(data, addr[1]) # data and port listen("localhost", listenPort) 
+5
source share

Another, but concomitant solution for port forwarding, and not multiplexing (not answering a specific question, but hopefully matching the corresponding ones) is what I was looking for, at least):

http://www.linux-support.com/cms/forward-network-connections-with-python/

0
source share

All Articles