10b237f3cSFilip Tehlarimport sys
20b237f3cSFilip Tehlarfrom scapy.all import *
30b237f3cSFilip Tehlar
40b237f3cSFilip Tehlardef p(s):
50b237f3cSFilip Tehlar    print 'DHCP client: {}'.format(s)
60b237f3cSFilip Tehlar
70b237f3cSFilip Tehlardef main(argv):
80b237f3cSFilip Tehlar  src_mac = argv[1]
90b237f3cSFilip Tehlar  dhcp_src = argv[2]
100b237f3cSFilip Tehlar
110b237f3cSFilip Tehlar  # needed for scapy not to match replies since DHCP uses broadcast addresses
120b237f3cSFilip Tehlar  # which wouldn't work
130b237f3cSFilip Tehlar  conf.checkIPaddr = False
140b237f3cSFilip Tehlar
150b237f3cSFilip Tehlar  while True:
160b237f3cSFilip Tehlar    discover = Ether(dst='ff:ff:ff:ff:ff:ff', src=src_mac)/ \
170b237f3cSFilip Tehlar      IP(src='0.0.0.0', dst='255.255.255.255')/ \
180b237f3cSFilip Tehlar      UDP(dport=67,sport=68)/ \
190b237f3cSFilip Tehlar      BOOTP(op=1, chaddr=src_mac)/ \
200b237f3cSFilip Tehlar      DHCP(options=[('message-type', 'discover'), ('end')])
210b237f3cSFilip Tehlar
220b237f3cSFilip Tehlar    ans,unans = srp(discover, timeout=3)
230b237f3cSFilip Tehlar    for snd,rcv in ans:
240b237f3cSFilip Tehlar      if rcv[IP].src == dhcp_src:
250b237f3cSFilip Tehlar        exit(0)
260b237f3cSFilip Tehlar      else:
270b237f3cSFilip Tehlar        p('Unexpected DHCP packet source address! ({})'.format(rcv[IP].src))
280b237f3cSFilip Tehlar        exit(1)
290b237f3cSFilip Tehlar
300b237f3cSFilip Tehlarif __name__ == "__main__":
310b237f3cSFilip Tehlar  main(sys.argv)
32