Advertisement
abdulfatirs

PortScanner.py | Basic Port Scanner

May 1st, 2014
542
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.  
  5. from socket import *
  6. HostName = raw_input("Host Name: ")
  7. try:
  8.     # gethostbyname(HostName) returns the IPv4 address of a website
  9.     IPaddress = gethostbyname(HostName)
  10. except:
  11.     print "[-] Could not find host."
  12.     quit()
  13. portstring = raw_input("Ports (separated by commas):")
  14. ports = portstring.split(",")
  15. print "\n[*] Scan results for "+HostName+" ("+IPaddress+"):"
  16. # 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.
  17. setdefaulttimeout(1)
  18. for port in ports:
  19.     try:
  20.         connection = socket(AF_INET,SOCK_STREAM)
  21.         # socket.connect((IP,PORT)) connect to the IP at given port.
  22.         connection.connect((IPaddress,int(port)))
  23.         connection.send('hello\r\n')
  24.         # socket.recv(BUFFER_SIZE) receives the server response
  25.         data = connection.recv(8)
  26.         # If we have reached this far that means the port is open.
  27.         print "\t[+] "+port+"/tcp open"
  28.     except:
  29.         print "\t[-] "+port+"/tcp closed"
  30.     finally:
  31.         connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement