ade_talon

Get Open Ports

Apr 8th, 2022 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. import os
  2.  
  3. os.system("cls||clear")     # Clear the terminal screen
  4.  
  5. stream = os.popen("netstat -lnt | grep tcp | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=\":\"} { print $(NF) }' | sort -n | uniq")      # Get all open TCP ports
  6. result = stream.read()
  7.  
  8. stream = os.popen("netstat -lnu | grep udp | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=\":\"} { print $(NF) }' | sort -n | uniq")      # Get all open UDP ports
  9. result += stream.read()
  10.  
  11. result = result.split("\n")     # Convert the result into a list
  12. result = " ".join(result).split()       # Remove empty elements, if there are any
  13. result.sort()       # Sorts the list alphabetically ascending
  14.  
  15. print(result)
Add Comment
Please, Sign In to add comment