matts0613

systemInfo.py

May 20th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. #script that displays public info about your IP, network, geolocation, hardware, etc.
  2. #wrote this just for practice writing python3 scripts
  3. #python3 systemInfo.py
  4.  
  5.  
  6. import socket
  7. from urllib.request import urlopen
  8. import re
  9. import json
  10. import sys, getopt
  11. import psutil
  12. import platform
  13. from datetime import datetime
  14. import time
  15. import argparse
  16.  
  17. #adds CLI usage for -h or --help
  18. msg = "Usage: python3 systemInfo.py"
  19. parser = argparse.ArgumentParser()
  20. parser = argparse.ArgumentParser(description = msg + "\n-----" + "\n\n\n\n\n This script prints information about your machine and network.")
  21. parser.parse_args()
  22.  
  23.  
  24. #get hostname
  25. hostname = socket.gethostname()
  26.  
  27. #get IP address  
  28. IPAddr = socket.gethostbyname(hostname)
  29.  
  30. print("="*30, "IP Information", "="*30)
  31.  
  32. #prints hostname  
  33. print("Your Computer Name is: " + hostname)
  34.  
  35. #prints IP address  
  36. print("Your Internal IP Address is: " + IPAddr)
  37.  
  38. def public_ip():
  39.     read_res = urlopen("http://ipecho.net/plain").read()
  40.     return read_res.decode("utf-8")
  41.  
  42. if __name__ == "__main__":
  43.     print("Getting public IP...")
  44.     print("Public IP: {} ".format(public_ip(), ))
  45.  
  46.  
  47. #gets geo info
  48. url = "http://ipinfo.io/json"
  49. #open url
  50. response = urlopen(url)
  51. #load json response data
  52. data = json.load(response)
  53.  
  54. #json response data
  55. IP=data["ip"]
  56. org=data["org"]
  57. city = data["city"]
  58. country=data["country"]
  59. region=data["region"]
  60.  
  61. #print line for better spacing
  62. print()
  63. time.sleep(2)
  64.  
  65.  
  66. print("Printing your geolocation information")
  67. print("="*30, "Geolocation Information", "="*30)
  68.  
  69. #Prints IP, region, country, city, org response data
  70. print("IP : {4} \nRegion : {1} \nCountry : {2} \nCity : {3} \nOrg : {0}".format(org,region,country,city,IP))
  71.  
  72. #print line for better spacing
  73. print()
  74.  
  75. time.sleep(2)
  76.  
  77. print("Printing your system details \n ")
  78. time.sleep(2)
  79. print("="*30, "System Information", "="*30)
  80.  
  81.  
  82. #prints general system info
  83. uname = platform.uname()
  84. print(f"System: {uname.system}")
  85. print(f"Node Name: {uname.node}")
  86. print(f"Release: {uname.release}")
  87. print(f"Version: {uname.version}")
  88. print(f"Machine: {uname.machine}")
  89. print(f"Processor: {uname.processor}")
  90.  
  91. #print how long since computer boot
  92. print("="*30, "Boot Time", "="*30)
  93. boot_time_timestamp = psutil.boot_time()
  94. bt = datetime.fromtimestamp(boot_time_timestamp)
  95. print(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")
  96.  
  97.  
  98. #print CPU cores on current machine
  99. print("="*30, "CPU Info", "="*30)
  100. # number of cores
  101. print("Physical cores:", psutil.cpu_count(logical=False))
  102. print("Total cores:", psutil.cpu_count(logical=True))
  103.  
  104. time.sleep(2)
  105.  
  106. print()
  107.  
  108. print("Printing your network interface details \n ")
  109. print("="*30, "Network Interfaces", "="*30)
  110.  
  111. time.sleep(2)
  112.  
  113. #prints network interface info
  114. if_addrs = psutil.net_if_addrs()
  115. for interface_name, interface_addresses in if_addrs.items():
  116.     for address in interface_addresses:
  117.         print(f"=== Interface: {interface_name} ===")
  118.         if str(address.family) == 'AddressFamily.AF_INET':
  119.             print(f"  IP Address: {address.address}")
  120.             print(f"  Netmask: {address.netmask}")
  121.             print(f"  Broadcast IP: {address.broadcast}")
  122.         elif str(address.family) == 'AddressFamily.AF_PACKET':
  123.             print(f"  MAC Address: {address.address}")
  124.             print(f"  Netmask: {address.netmask}")
  125.             print(f"  Broadcast MAC: {address.broadcast}")
Add Comment
Please, Sign In to add comment