-
Notifications
You must be signed in to change notification settings - Fork 102
/
dhscript.py
executable file
·112 lines (99 loc) · 4.79 KB
/
dhscript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
import sys
import os
if sys.version_info.major <= 2:
from commands import getoutput
else:
import subprocess
# subprocess.getoutput would not handle binary output
def getoutput(cmd):
try:
out = subprocess.run(cmd, text=False, shell=True,
timeout=60, stdout=subprocess.PIPE)
return out.stdout
except subprocess.TimeoutExpired as e:
return b'ERROR: Timeout'
#reset script log file
with open('dhscript_log.txt', 'w') as f:
f.write('')
f.close
#print_log(- prints the output to both stdout and file
def print_log(msg, cmd=None):
if cmd != None:
msg_full = msg + cmd + "\n"
else:
msg_full = msg + "\n"
sys.stdout.write(msg_full)
with open('dhscript_log.txt', 'a') as f:
f.write(msg_full)
f.close
#write_log - write the output to file
def write_log(msg):
with open('dhscript_log.txt', 'ab') as f:
msg_full = msg + b"\n"
f.write(msg_full)
f.close
def run_dhtest(mac, arg_list, search_output):
cmd = "./dhtest -i eth0 -m " + mac + arg_list
print_log("=============================================================")
print_log("Running command ", cmd)
out = getoutput(cmd)
write_log(out)
if isinstance(out, bytes):
search_output = search_output.encode('ascii')
if out.find(search_output) != -1:
print_log("PASS: command ", cmd)
else:
print_log("FAIL: command ", cmd)
print_log("============================================================")
# OPTION LIST
# ============
# -r, --release # Releases obtained DHCP IP for corresponding MAC
# -L, --option51-lease_time [ Lease_time ] # Option 51. Requested lease time in secondes
# -I, --option50-ip [ IP_address ] # Option 50 IP address on DHCP discover
# -o, --option60-vci [ VCI_string ] # Vendor Class Idendifier string
# -h, --option12-hostname [ hostname_string ] # Client hostname string
# -v, --vlan [ vlan_id ] # VLAN ID. Range(1 - 4094)
# -t, --tos [ TOS_value ] # IP header TOS value
# -i, --interface [ interface ] # Interface to use. Default eth0
# -T, --timeout [ cmd_timeout ] # Command returns within specified timout in seconds
# -b, --bind-ip # Listens on the obtained IP. Supported protocols - ARP and ICMP
# -k, --bind-timeout [ timeout ] # Listen timout in seconds. Default 3600 seconds
# -f, --bcast_flag # Sets broadcast flag on DHCP discover and request
# -d, --fqdn-domain-name [ fqdn ] # FQDN domain name to use
# -n, --fqdn-server-not-update # Sets FQDN server not update flag
# -s, --fqdn-server-update-a # Sets FQDN server update flag
# -p, --padding # Add padding to packet to be at least 300 bytes
# -P, --port [ port ] # Use port instead of 67
# -g, --giaddr [ giaddr ] # Use giaddr instead of 0.0.0.0
# -u, --unicast [ ip ] # Unicast request, IP is optional. If not specified, the interface address will be used.
# -a, --nagios # Nagios output format.
# -S, --server [ address ] # Use server address instead of 255.255.255.255
# -D, --decline # Declines obtained DHCP IP for corresponding MAC
# -V, --verbose # Prints DHCP offer and ack details
mac = "00:00:00:11:11:11"
run_dhtest(mac, '', "DHCP ack received")
run_dhtest(mac, ' -V', "DHCP ack received")
run_dhtest(mac, ' -r', "DHCP release sent")
run_dhtest(mac, ' -L 1200', "DHCP ack received")
run_dhtest(mac, ' -I 10.0.2.16', "DHCP ack received")
run_dhtest(mac, ' -o MSFT 5.0', "DHCP ack received")
run_dhtest(mac, ' -h client_hostname', "DHCP ack received")
run_dhtest(mac, ' -t 10', "DHCP ack received")
run_dhtest(mac, ' -i eth0', "DHCP ack received")
run_dhtest(mac, ' -T 60', "DHCP ack received")
run_dhtest(mac, ' -b -k 10', "DHCP ack received")
run_dhtest(mac, ' -r', "DHCP release sent")
run_dhtest(mac, ' -f ', "DHCP ack received")
run_dhtest(mac, ' -d client.test.com ', "DHCP ack received")
run_dhtest(mac, ' -n', "DHCP ack received")
run_dhtest(mac, ' -s', "DHCP ack received")
run_dhtest(mac, ' -p', "DHCP ack received")
run_dhtest(mac, ' -g 10.0.2.1', "DHCP ack received")
run_dhtest(mac, ' -a', "Acquired IP")
run_dhtest(mac, ' -c 60,str,"MSFT 5.0" -c 82,hex,0108476967302f312f30021130303a30303a30303a31313a31313a3131 ', "DHCP ack received")
run_dhtest(mac, ' -D', "DHCP decline sent")
run_dhtest(mac, ' -l 011C030F060A0B111121314151617181192021222232425262728292a2b2c2d2e2', "DHCP ack received")
if os.getenv('DNSMASQ_TEST') is None:
# Skip test that always fail under dnsmasq test
run_dhtest(mac, ' -S 10.0.2.2 ', "DHCP ack received")