28 lines
695 B
Python
28 lines
695 B
Python
import socket
|
|
|
|
def _resolve_hostname(ip):
|
|
"""Reverse DNS lookup for a single IP."""
|
|
try:
|
|
return socket.gethostbyaddr(ip)[0]
|
|
except Exception:
|
|
return ""
|
|
|
|
def get_local_ip():
|
|
"""Get the local IP address of this machine."""
|
|
try:
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
s.connect(("8.8.8.8", 80))
|
|
ip = s.getsockname()[0]
|
|
s.close()
|
|
return ip
|
|
except Exception:
|
|
return "N/A"
|
|
|
|
def get_default_network(ip):
|
|
"""Guess the /24 network from local IP."""
|
|
try:
|
|
parts = ip.split(".")
|
|
return f"{parts[0]}.{parts[1]}.{parts[2]}.0/24"
|
|
except Exception:
|
|
return "192.168.1.0/24"
|