# Function to gather system information
def get_system_info():
try:
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
public_ip = requests.get("https://api64.ipify.org?format=json").json().get("ip", "Unavailable")
mac = ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff)for i in range(0, 8 * 6, 8)][::-1])
system = platform.system()
release = platform.release()
version = platform.version()
architecture = platform.architecture()[0]
processor = platform.processor()
return f"""
System Number/ID: {system_number}
Hostname: {hostname}
Local IP Address: {local_ip}
Public IP Address: {public_ip}
MAC Address: {mac}
Operating System: {system} {release}
OS Version: {version}
Architecture: {architecture}
Processor: {processor}
"""
except Exception as e:
return f"Error retrieving system info: {str(e)}"
Explanation:
This Python function, get_system_info, retrieves and displays various details about the computer's system and network configuration. Here's a brief explanation of its components:
Hostname: Obtains the device's hostname using socket.gethostname().
Local IP Address: Fetches the IP address of the machine on the local network using socket.gethostbyname().
Public IP Address: Sends an HTTP request to an external API (https://api64.ipify.org) to retrieve the machine's public IP address. The API responds with the public IP in JSON format, and .json().get("ip") extracts it. If unavailable, it defaults to "Unavailable".
MAC Address: Computes the MAC address (unique hardware identifier) by processing uuid.getnode() (a unique identifier for the network interface). The MAC address is formatted as a human-readable hexadecimal string.
Operating System Information:
platform.system(): Retrieves the name of the operating system (e.g., Windows, Linux).
platform.release(): Gets the release version of the operating system (e.g., 10 for Windows 10).
platform.version(): Provides detailed version information about the operating system.
platform.architecture()[0]: Indicates the architecture of the system (e.g., 32-bit or 64-bit).
platform.processor(): Retrieves the processor type or name.
Formatted Output: All gathered information is organized and returned as a formatted string.
Error Handling: If an error occurs during the information retrieval (e.g., network issues, API failure), the exception is caught and a relevant error message is returned.