#!/usr/bin/python

import libnet
import libnet
import string

src_ip=libnet.name_resolve('192.168.1.2',0)
tgt_ip=libnet.name_resolve('192.168.1.1',0)

def dumphex(s):
    bytes = map(lambda x: '%.2x ' % x, map(ord, s))
    if len(s)>16:
      for i in range((len(s)+16)/16*16, 15, -16):
        bytes.insert(i,'\n')
    print string.join(bytes, '')


if __name__=="__main__":

  ifc = libnet.interface()
  ifc.open_raw(libnet.IPPROTO_RAW)

  pkt = libnet.packet()
  pkt.build_tcp(1025,
                80,
                1,
                0,
                libnet.TH_SYN,
                0,
                0)
  dumphex(pkt.getvalue())

  pkt.build_ip(len(pkt),
               0,
               1,
               0,
               255,
               libnet.IPPROTO_TCP,
               src_ip,
               tgt_ip)
                            
  dumphex(pkt.getvalue())
  pkt.do_checksum(libnet.IPPROTO_TCP, libnet.TCP_H)
  dumphex(pkt.getvalue())

  print 'tcp syn:'
  dumphex(pkt.getvalue())

  ifc.write(pkt)


syntax highlighted by Code2HTML, v. 0.9.1