Guest User

portscan.py

a guest
May 6th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import socket
  2. import optparse
  3.  
  4. """Simple portscan, HackForLulz"""
  5.  
  6. def portscan(h, p, options):
  7.  
  8. try:
  9. conn = socket.socket("AF_INET", "SOCK_STREAM")
  10. if options.timeout is None:
  11. conn.setdefaulttimeout(3)
  12. else:
  13. conn.setdefaulttimeout(options.timeout)
  14. conn.connect((h.rstrip(), int(p)))
  15. print "[+] %d port open" % int(p)
  16. except:
  17. print "[-] %d port closed" % int(p)
  18.  
  19.  
  20. def scanfile(options,b):
  21. for x in b.readlines():
  22. if x=="\n":
  23. continue
  24. try:
  25. h = socket.gethostbyname(x.rstrip())
  26. except:
  27. print "[-] Cannot resolve %s : Unknown host" %x.rstrip()
  28. continue
  29. p = (options.port).split(",")
  30. print "[+] Scan %s :"%x.rstrip()
  31. socket.setdefaulttimeout(2)
  32. for y in p:
  33. print "[+] Scanning port ",y
  34. portscan(x,y,options)
  35. print "\n"
  36.  
  37. def scan(options):
  38. try:
  39. h = socket.gethostbyname(options.host)
  40. except:
  41. print "[-] Cannot resolve %s : Unknown host" % h
  42. return
  43. p = (options.port).split(",")
  44. print "[+] Scan %s\n" % h
  45. socket.setdefaulttimeout(2)
  46. for x in p:
  47. print "[+] Scanning port ", x
  48. portscan(h, x, options)
  49. print "\n"
  50.  
  51. def main():
  52. p = optparse.OptionParser("usage %prog -H <host> -p <port> [-F <file>]")
  53. p.add_option("-H", "--host", dest="host", type="string", help="target host")
  54. p.add_option("-p", "--port", dest="port", type="string", help="target port")
  55. p.add_option("-f", "--file", dest="file", type="string",help="target file")
  56. p.add_option("-t", "--timeout", dest="timeout", type="int",help="timeout in second (default 3s)")
  57. (options,args) = p.parse_args()
  58. if(options.host is None and options.file is None):
  59. return
  60. elif(options.port is None):
  61. return
  62. elif(options.host is not None and options.file is not None):
  63. return
  64. elif(options.file is not None):
  65. b=open(options.file,"r")
  66. scanfile(options,b)
  67. else:
  68. scan(options)
  69.  
  70. if __name__ == "__main__":
  71. main()
Advertisement
Add Comment
Please, Sign In to add comment