Send a DHCP discover request and return the answer. Usage:: >>> dhcp_request() # send DHCP discover >>> dhcp_request(req_type='request', ... requested_addr='10.53.4.34') # send DHCP request
(hw=None,
req_type='discover',
server_id=None,
requested_addr=None,
hostname=None,
iface=None,
**kargs)
| 551 | |
| 552 | @conf.commands.register |
| 553 | def dhcp_request(hw=None, |
| 554 | req_type='discover', |
| 555 | server_id=None, |
| 556 | requested_addr=None, |
| 557 | hostname=None, |
| 558 | iface=None, |
| 559 | **kargs): |
| 560 | """ |
| 561 | Send a DHCP discover request and return the answer. |
| 562 | |
| 563 | Usage:: |
| 564 | |
| 565 | >>> dhcp_request() # send DHCP discover |
| 566 | >>> dhcp_request(req_type='request', |
| 567 | ... requested_addr='10.53.4.34') # send DHCP request |
| 568 | """ |
| 569 | if conf.checkIPaddr: |
| 570 | warning( |
| 571 | "conf.checkIPaddr is enabled, may not be able to match the answer" |
| 572 | ) |
| 573 | if hw is None: |
| 574 | if iface is None: |
| 575 | iface = conf.iface |
| 576 | hw = get_if_hwaddr(iface) |
| 577 | dhcp_options = [ |
| 578 | ('message-type', req_type), |
| 579 | ('client_id', b'\x01' + mac2str(hw)), |
| 580 | ] |
| 581 | if requested_addr is not None: |
| 582 | dhcp_options.append(('requested_addr', requested_addr)) |
| 583 | elif req_type == 'request': |
| 584 | warning("DHCP Request without requested_addr will likely be ignored") |
| 585 | if server_id is not None: |
| 586 | dhcp_options.append(('server_id', server_id)) |
| 587 | if hostname is not None: |
| 588 | dhcp_options.extend([ |
| 589 | ('hostname', hostname), |
| 590 | ('client_FQDN', b'\x00\x00\x00' + bytes_encode(hostname)), |
| 591 | ]) |
| 592 | dhcp_options.extend([ |
| 593 | ('vendor_class_id', b'MSFT 5.0'), |
| 594 | ('param_req_list', [ |
| 595 | 1, 3, 6, 15, 31, 33, 43, 44, 46, 47, 119, 121, 249, 252 |
| 596 | ]), |
| 597 | 'end' |
| 598 | ]) |
| 599 | return srp1( |
| 600 | Ether(dst="ff:ff:ff:ff:ff:ff", src=hw) / |
| 601 | IP(src="0.0.0.0", dst="255.255.255.255") / |
| 602 | UDP(sport=68, dport=67) / |
| 603 | BOOTP(chaddr=hw, xid=RandInt(), flags="B") / |
| 604 | DHCP(options=dhcp_options), |
| 605 | iface=iface, **kargs |
| 606 | ) |
| 607 | |
| 608 | |
| 609 | class BOOTP_am(AnsweringMachine): |
nothing calls this directly
no test coverage detected
searching dependent graphs…