Advertisement
abdulfatirs

PortScanner.py | Basic Port Scanner

May 1st, 2014
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # PortScanner.py
  3. # Author: Abdul Fatir
  4. # E-Mail: abdulfatirs@gmail.com
  5.  
  6. from socket import *
  7. HostName = raw_input("Host Name: ")
  8. try:
  9.     # gethostbyname(HostName) returns the IPv4 address of a website
  10.     IPaddress = gethostbyname(HostName)
  11. except:
  12.     print "[-] Could not find host."
  13.     quit()
  14. portstring = raw_input("Ports (separated by commas):")
  15. ports = portstring.split(",")
  16. print "\n[*] Scan results for "+HostName+" ("+IPaddress+"):"
  17. # setdefaulttimeout(1) sets the waiting time for website response to 1 seconds. This may result in closed port even though the port may be open if it takes more than 1 second.
  18. setdefaulttimeout(1)
  19. for port in ports:
  20.     try:
  21.         connection = socket(AF_INET,SOCK_STREAM)
  22.         # socket.connect((IP,PORT)) connect to the IP at given port.
  23.         connection.connect((IPaddress,int(port)))
  24.         connection.send('hello\r\n')
  25.         # socket.recv(BUFFER_SIZE) receives the server response
  26.         data = connection.recv(8)
  27.         # If we have reached this far that means the port is open.
  28.         print "\t[+] "+port+"/tcp open"
  29.     except:
  30.         print "\t[-] "+port+"/tcp closed"
  31.     finally:
  32.         connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement