SHARE
TWEET

Port Tester v0.1, firewall port testing tool

a guest Feb 2nd, 2012 176 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. # Contact: mattdch0@gmail.com
  8. # Follow: @mattdch
  9.  
  10. import sys,socket,errno
  11.  
  12. # Functions goes here
  13.  
  14. def banner():
  15.         return "\n####################\n# Port Tester v0.1 #\n####################"
  16.  
  17. def exitProgram(code):
  18.         if code==1:
  19.                 sys.exit("\n[!] Exiting help...\n")
  20.         if code==2:
  21.                 sys.exit("\n[!] Test finished, exiting...\n")
  22.         if code==3:
  23.                 sys.exit("\n[!] Exiting...\n")
  24.         if code==4:
  25.                 sys.exit("\n[-] Exiting, check arguments...\n")
  26.  
  27. def strToInt(convert,typeParam):
  28.         try:
  29.                 value = int(convert)
  30.                 return value
  31.         except:
  32.                 print "\n[-] Number given in " + typeParam + " is invalid"
  33.                 exitProgram(3)
  34.  
  35. def checkTimeout(timeout):
  36.         if timeout is None or timeout <= 0:
  37.                 # Default timeout : 3 seconds
  38.                 timeout = 3
  39.         else:
  40.                 pass
  41.         return timeout
  42.  
  43. def connectHost(host,port,timeout):
  44.         try:
  45.                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46.                 sock.settimeout(timeout)
  47.                 sock.connect((host, port))
  48.                 # Connection established, we can access that port
  49.                 return "[+] We can reach port " + str(port)
  50.         except:
  51.                 # If some error happens (refused / filtered), we cannot access that port, print that
  52.                 return "[-] We cannot reach port " + str(port)
  53.  
  54. if len(sys.argv) <= 4:
  55.         print banner()
  56.         print "\nUsage:\n======\n\npython", sys.argv[0], "-s [START PORT] -e [END PORT] -t [TIMEOUT (Seconds) (Optional, default: 3)]"
  57.         exitProgram(1)
  58.  
  59. # Set some variables
  60. count = 0
  61. timeout = None
  62. start_port = None
  63. end_port = None
  64.  
  65. # Read args
  66. for arg in sys.argv:
  67.         if arg == "-s":
  68.                 start_port = strToInt(sys.argv[count+1],"-s")
  69.         elif arg == "-e":
  70.                 end_port = strToInt(sys.argv[count+1],"-e")
  71.         elif arg == "-t":
  72.                 timeout = strToInt(sys.argv[count+1],"-t")
  73.         count+=1
  74.  
  75. # Do some checks
  76. if start_port is None or end_port is None:
  77.         exitProgram(4)
  78. timeout = checkTimeout(timeout)
  79.  
  80. # Test started
  81. print banner()
  82. print "\n[!] Port-test started..."
  83. print "[!] Timeout: " + str(timeout) + " seconds\n"
  84.  
  85. # 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")
  86. hostname = '67.215.250.139' # open.zorinaq.com , 65k ports open
  87.  
  88. for port in range(start_port , end_port+1):
  89.         print connectHost(hostname, port, timeout)
  90. exitProgram(2)
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top