Skip to content

Instantly share code, notes, and snippets.

Created Sep 28, 2013
What would you like to do?
Send UDP packets from any custom port (using libnet). Used for UDP hole punching on linux servers (when the local UDP port is being used).
#!/usr/bin/env python
#encoding: utf-8
import libnet
from libnet.constants import RAW4, RESOLVE, IPV4_H, UDP_H, IPPROTO_UDP
IFACE = 'wlan2' # Sending via the interface.
def sendto(sport, address):
l = libnet.context(RAW4, IFACE)
dest_ip = l.name2addr4(address[0], RESOLVE)
l.build_udp(sp=sport, dp=address[1],
l.autobuild_ipv4(len=(IPV4_H + UDP_H),
prot=IPPROTO_UDP, dst=dest_ip)
if __name__ == '__main__':
sendto(6000, ('', 6001))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment