Advertisement
Tigron1901

dsfds

Apr 17th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import subprocess
  2.  
  3. def get_local_ip():
  4. """Get the local IP address of the PC."""
  5. try:
  6. # Use subprocess to execute 'ipconfig' command on Windows or 'ifconfig' command on Unix-based systems
  7. result = subprocess.run(['ipconfig'], capture_output=True, text=True)
  8. if result.returncode == 0:
  9. output = result.stdout
  10. # Extracting IPv4 address from the output
  11. local_ip_index = output.find('IPv4 Address')
  12. if local_ip_index != -1:
  13. local_ip_start = output.find(':', local_ip_index)
  14. local_ip_end = output.find('\n', local_ip_start)
  15. local_ip = output[local_ip_start + 2: local_ip_end].strip()
  16. return local_ip
  17. else:
  18. print("Error:", result.stderr)
  19. except Exception as e:
  20. print("Error:", e)
  21. return None
  22.  
  23. local_ip = get_local_ip()
  24.  
  25. if local_ip:
  26. print("Local IP:", local_ip)
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement