4 from pydhcplib import dhcp_constants
5 from pydhcplib import dhcp_packet
6 from pydhcplib import dhcp_network
8 from pydhcplib import type_hw_addr
9 from pydhcplib import type_ipv4
10 from pydhcplib import type_strlist
14 from optparse import OptionParser
17 parser = OptionParser()
19 """ Action options """
20 parser.add_option("-L", "--listen", action="store_true",dest="listen", help="",default=False)
21 parser.add_option("-E", "--emit", action="store_true",dest="emit", help="", default=False)
22 parser.add_option("-R", "--readable-conversion", action="store_true",dest="readable", help="", default=False)
23 parser.add_option("-B", "--binary-conversion", action="store_true",dest="binary", help="", default=False)
24 parser.add_option("-s", "--source-file", action="store",dest="source", help="", default=False, type="string")
25 parser.add_option("-d", "--destination-file", action="store",dest="destination", help="", default=False, type="string")
26 parser.add_option("-p", "--port", action="store",dest="port", help="", default="67", type="int")
27 parser.add_option("-a", "--address", action="store",dest="address", help="", default="0.0.0.0", type="string")
28 parser.add_option("-r", "--raw", action="store",dest="raw", help="", default=False,type="string")
29 parser.add_option("-n", "--number", action="store",dest="number", help="", default="0", type="int")
33 (options, args) = parser.parse_args()
41 for Action in (options.listen,options.emit,options.readable,options.binary) :
42 if Action == True : ActionSum += 1
44 print "Command line error : [-L -E -R -B] Only one of these actions can be taken."
47 if options.readable == True : r_conversion()
49 if options.binary == True : b_conversion()
51 if options.listen == True :
52 if options.raw == False:
53 listen_address(options.address,options.port,int(options.number))
55 print "Listen RAW : ",options.raw
56 listen_packet(options.raw,options.number)
58 if options.emit == True : emit(options.address,options.port)
61 def listen_address(address,port,number) :
62 listener = dhcp_network.DhcpClient(address,port,port)
66 packet = listener.GetNextDhcpPacket()
72 packet = listener.GetNextDhcpPacket()
78 def listen_packet(interface,number) :
79 listener = dhcp_network.DhcpRawClient(mysocket)
83 packet = dhcp_packet.DhcpPacket()
85 packet.DecodePacket(listener.Receive(1024))
91 packet = dhcp_packet.DhcpPacket()
93 packet.DecodePacket(listener.Receive(1024))
100 def emit(address,port) :
104 rawdata = sys.stdin.read()
105 while ( len(rawdata)>0 ) :
106 readdata = dhcp_packet.DhcpPacket()
107 readdata.DecodePacket(rawdata)
108 readdata.PrintHeaders()
109 readdata.PrintOptions()
110 rawdata = sys.stdin.read()
114 pythondata = sys.stdin.read()
115 while ( len(pythondata)>0 ) :
116 data = dhcp_packet.DhcpPacket()
117 data.DecodePacket(rawdata)
119 pythondata = sys.stdin.read()