| 643 | return False |
| 644 | |
| 645 | def get(self, arg): # type: (str) -> Optional[str] |
| 646 | if not PY2: # Convert to bytes on Python 3+ (Fixes GitHub issue #7) |
| 647 | arg = arg.encode() # type: ignore |
| 648 | |
| 649 | try: |
| 650 | inetaddr = ctypes.windll.wsock32.inet_addr(arg) # type: ignore |
| 651 | if inetaddr in (0, -1): |
| 652 | raise Exception |
| 653 | except Exception: |
| 654 | # TODO: this assumes failure is due to arg being a hostname |
| 655 | # We should be explicit about only accepting ipv4 addresses |
| 656 | # and handle any hostname resolution in calling code |
| 657 | hostip = socket.gethostbyname(arg) |
| 658 | inetaddr = ctypes.windll.wsock32.inet_addr(hostip) # type: ignore |
| 659 | |
| 660 | buffer = ctypes.c_buffer(6) |
| 661 | addlen = ctypes.c_ulong(ctypes.sizeof(buffer)) |
| 662 | |
| 663 | # https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-sendarp |
| 664 | send_arp = ctypes.windll.Iphlpapi.SendARP # type: ignore |
| 665 | if send_arp(inetaddr, 0, ctypes.byref(buffer), ctypes.byref(addlen)) != 0: |
| 666 | return None |
| 667 | |
| 668 | # Convert binary data into a string. |
| 669 | macaddr = "" |
| 670 | for intval in struct.unpack("BBBBBB", buffer): # type: ignore |
| 671 | if intval > 15: |
| 672 | replacestr = "0x" |
| 673 | else: |
| 674 | replacestr = "x" |
| 675 | macaddr = "".join([macaddr, hex(intval).replace(replacestr, "")]) |
| 676 | |
| 677 | return macaddr |
| 678 | |
| 679 | |
| 680 | class IpNeighborShow(Method): |