MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / _fetch_ip_using_dns

Function _fetch_ip_using_dns

pager_lib/getmac/getmac.py:295–308  ·  view source on GitHub ↗

Determines the IP address of the default network interface. Sends a UDP packet to Cloudflare's DNS (``1.1.1.1``), which should go through the default interface. This populates the source address of the socket, which we then inspect and return.

()

Source from the content-addressed store, hash-verified

293
294
295def _fetch_ip_using_dns():
296 # type: () -> str
297 """
298 Determines the IP address of the default network interface.
299
300 Sends a UDP packet to Cloudflare's DNS (``1.1.1.1``), which should go through
301 the default interface. This populates the source address of the socket,
302 which we then inspect and return.
303 """
304 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
305 s.connect(("1.1.1.1", 53))
306 ip = s.getsockname()[0]
307 s.close() # NOTE: sockets don't have context manager in 2.7 :(
308 return ip
309
310
311class Method:

Callers 1

get_mac_addressFunction · 0.85

Calls 2

connectMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected