Guest User

Port Tester v0.1, firewall port testing tool

a guest
Feb 2nd, 2012
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Port Tester v0.1
  4. # Test which remote ports the server running this script can access to.
  5. # written by localh0t
  6. # Date: 03/02/12
  7. # Follow: @mattdch
  8.  
  9. import sys,socket,errno
  10.  
  11. # Functions goes here
  12.  
  13. def banner():
  14.     return "\n####################\n# Port Tester v0.1 #\n####################"
  15.  
  16. def exitProgram(code):
  17.     if code==1:
  18.         sys.exit("\n[!] Exiting help...\n")
  19.     if code==2:
  20.         sys.exit("\n[!] Test finished, exiting...\n")
  21.     if code==3:
  22.         sys.exit("\n[!] Exiting...\n")
  23.     if code==4:
  24.         sys.exit("\n[-] Exiting, check arguments...\n")
  25.  
  26. def strToInt(convert,typeParam):
  27.     try:
  28.         value = int(convert)
  29.         return value
  30.     except:
  31.         print "\n[-] Number given in " + typeParam + " is invalid"
  32.         exitProgram(3)
  33.  
  34. def checkTimeout(timeout):
  35.     if timeout is None or timeout <= 0:
  36.         # Default timeout : 3 seconds
  37.         timeout = 3
  38.     else:
  39.         pass
  40.     return timeout
  41.  
  42. def connectHost(host,port,timeout):
  43.     try:
  44.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  45.         sock.settimeout(timeout)
  46.         sock.connect((host, port))
  47.         # Connection established, we can access that port
  48.         return "[+] We can reach port " + str(port)
  49.     except:
  50.         # If some error happens (refused / filtered), we cannot access that port, print that
  51.         return "[-] We cannot reach port " + str(port)
  52.  
  53. if len(sys.argv) <= 4:
  54.     print banner()
  55.     print "\nUsage:\n======\n\npython", sys.argv[0], "-s [START PORT] -e [END PORT] -t [TIMEOUT (Seconds) (Optional, default: 3)]"
  56.     exitProgram(1)
  57.  
  58. # Set some variables
  59. count = 0
  60. timeout = None
  61. start_port = None
  62. end_port = None
  63.  
  64. # Read args
  65. for arg in sys.argv:
  66.     if arg == "-s":
  67.         start_port = strToInt(sys.argv[count+1],"-s")
  68.     elif arg == "-e":
  69.         end_port = strToInt(sys.argv[count+1],"-e")
  70.     elif arg == "-t":
  71.         timeout = strToInt(sys.argv[count+1],"-t")
  72.     count+=1
  73.  
  74. # Do some checks
  75. if start_port is None or end_port is None:
  76.     exitProgram(4)
  77. timeout = checkTimeout(timeout)
  78.  
  79. # Test started
  80. print banner()
  81. print "\n[!] Port-test started..."
  82. print "[!] Timeout: " + str(timeout) + " seconds\n"
  83.  
  84. # In case we had DNS problems on the server, we use the IP instead the domain, if you wanna use the domain : hostname = socket.gethostbyname("open.zorinaq.com")
  85. hostname = '67.215.250.139' # open.zorinaq.com , 65k ports open
  86.  
  87. for port in range(start_port , end_port+1):
  88.     print connectHost(hostname, port, timeout)
  89. exitProgram(2)
Advertisement
Add Comment
Please, Sign In to add comment