dhcp_client.py revision 0b237f3c
1import sys
2from scapy.all import *
3
4def p(s):
5    print 'DHCP client: {}'.format(s)
6
7def main(argv):
8  src_mac = argv[1]
9  dhcp_src = argv[2]
10
11  # needed for scapy not to match replies since DHCP uses broadcast addresses
12  # which wouldn't work
13  conf.checkIPaddr = False
14
15  while True:
16    discover = Ether(dst='ff:ff:ff:ff:ff:ff', src=src_mac)/ \
17      IP(src='0.0.0.0', dst='255.255.255.255')/ \
18      UDP(dport=67,sport=68)/ \
19      BOOTP(op=1, chaddr=src_mac)/ \
20      DHCP(options=[('message-type', 'discover'), ('end')])
21
22    ans,unans = srp(discover, timeout=3)
23    for snd,rcv in ans:
24      if rcv[IP].src == dhcp_src:
25        exit(0)
26      else:
27        p('Unexpected DHCP packet source address! ({})'.format(rcv[IP].src))
28        exit(1)
29
30if __name__ == "__main__":
31  main(sys.argv)
32