Advertisement
DrAungWinHtut

findmyip.py

Apr 30th, 2023
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import subprocess
  2. import re
  3.  
  4. # Define the command to run
  5. command = "ipconfig /all"
  6.  
  7. # Run the command using subprocess
  8. result = subprocess.run(command, capture_output=True, text=True, shell=True)
  9.  
  10. # Check if the command executed successfully
  11. if result.returncode == 0:
  12.     print("Command executed successfully:")
  13.     output = result.stdout
  14.  
  15.     # Extract IPv4 addresses using regular expressions
  16.     ip_pattern = re.compile(
  17.         r'IPv4 Address[ .]+: (?P<ip_address>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)')
  18.     ip_addresses = ip_pattern.findall(output)
  19.  
  20.     if ip_addresses:
  21.         print("IPv4 Addresses:")
  22.         for ip_address in ip_addresses:
  23.             print(ip_address)
  24.     else:
  25.         print("No IPv4 addresses found.")
  26. else:
  27.     print("Command execution failed:")
  28.     print(result.stderr)
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement