1 | #!/usr/bin/python |
---|
2 | |
---|
3 | |
---|
4 | from pydhcplib import dhcp_constants |
---|
5 | from pydhcplib import dhcp_packet |
---|
6 | from pydhcplib import dhcp_network |
---|
7 | |
---|
8 | from pydhcplib import type_hw_addr |
---|
9 | from pydhcplib import type_ipv4 |
---|
10 | from pydhcplib import type_strlist |
---|
11 | |
---|
12 | import sys |
---|
13 | |
---|
14 | from optparse import OptionParser |
---|
15 | |
---|
16 | |
---|
17 | parser = OptionParser() |
---|
18 | |
---|
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") |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | (options, args) = parser.parse_args() |
---|
34 | |
---|
35 | print options |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | def main() : |
---|
40 | ActionSum = 0 |
---|
41 | for Action in (options.listen,options.emit,options.readable,options.binary) : |
---|
42 | if Action == True : ActionSum += 1 |
---|
43 | if ActionSum > 1 : |
---|
44 | print "Command line error : [-L -E -R -B] Only one of these actions can be taken." |
---|
45 | sys.exit(0) |
---|
46 | |
---|
47 | if options.readable == True : r_conversion() |
---|
48 | |
---|
49 | if options.binary == True : b_conversion() |
---|
50 | |
---|
51 | if options.listen == True : |
---|
52 | if options.raw == False: |
---|
53 | listen_address(options.address,options.port,int(options.number)) |
---|
54 | else : |
---|
55 | print "Listen RAW : ",options.raw |
---|
56 | listen_packet(options.raw,options.number) |
---|
57 | |
---|
58 | if options.emit == True : emit(options.address,options.port) |
---|
59 | |
---|
60 | |
---|
61 | def listen_address(address,port,number) : |
---|
62 | listener = dhcp_network.DhcpClient(address,port,port) |
---|
63 | |
---|
64 | if (number == 0 ) : |
---|
65 | while True : |
---|
66 | packet = listener.GetNextDhcpPacket() |
---|
67 | packet.PrintHeaders() |
---|
68 | packet.PrintOptions() |
---|
69 | |
---|
70 | else : |
---|
71 | while number > 0 : |
---|
72 | packet = listener.GetNextDhcpPacket() |
---|
73 | packet.PrintHeaders() |
---|
74 | packet.Print() |
---|
75 | |
---|
76 | number -= 1 |
---|
77 | |
---|
78 | def listen_packet(interface,number) : |
---|
79 | listener = dhcp_network.DhcpRawClient(mysocket) |
---|
80 | |
---|
81 | if (number == 0 ) : |
---|
82 | while True : |
---|
83 | packet = dhcp_packet.DhcpPacket() |
---|
84 | |
---|
85 | packet.DecodePacket(listener.Receive(1024)) |
---|
86 | packet.PrintHeaders() |
---|
87 | packet.PrintOptions() |
---|
88 | |
---|
89 | else : |
---|
90 | while number > 0 : |
---|
91 | packet = dhcp_packet.DhcpPacket() |
---|
92 | |
---|
93 | packet.DecodePacket(listener.Receive(1024)) |
---|
94 | packet.PrintHeaders() |
---|
95 | packet.PrintOptions() |
---|
96 | |
---|
97 | number -= 1 |
---|
98 | |
---|
99 | |
---|
100 | def emit(address,port) : |
---|
101 | pass |
---|
102 | |
---|
103 | def r_conversion() : |
---|
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() |
---|
111 | |
---|
112 | def b_conversion() : |
---|
113 | """ |
---|
114 | pythondata = sys.stdin.read() |
---|
115 | while ( len(pythondata)>0 ) : |
---|
116 | data = dhcp_packet.DhcpPacket() |
---|
117 | data.DecodePacket(rawdata) |
---|
118 | |
---|
119 | pythondata = sys.stdin.read() |
---|
120 | """ |
---|
121 | |
---|
122 | main() |
---|