Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #=====================================#
- #==============MODULES================#
- #=====================================#
- from datetime import *
- import socket
- #=====================================#
- #==============VARIABLES==============#
- #=====================================#
- start = 0
- end = 0
- menus = ["(1) Scan by IP", "(2) Scan by Hostname", "(3) Exit "]
- line = "=" * 55
- #=====================================#
- #==============FUNCTIONS==============#
- #=====================================#
- #defines menu
- def menu(parameter):
- print " +====================+"
- print " | PORT SCANNER MENU |"
- print " |====================|"
- for word in parameter:
- print " |%s|" % word
- print " |-------------------|"
- def elapsed(start, end):
- final = (end - start)
- print "Scan Completed In: %s" % final
- def scanner(ip, line):
- low_port = input("Please enter a low port number: ")
- high_port = input("Please enter a high port number: ")
- hostname = socket.gethostbyaddr(ip) #get hostname from ip
- #change variable start to current time
- global start
- start = datetime.now()
- #create info bar
- print
- print line * 50
- print "Scanning IP: %s Hostname: %s" % (ip, hostname)
- print line * 50
- #iterate through ports and see if socket connection is established
- for port in range(low_port, (high_port + 1)):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- result = s.connect_ex((ip, port))
- if result == 0:
- print "Port %s: Open" % port
- s.close()
- #change variable end to current time
- global end
- end = datetime.now()
- #=====================================#
- #=============MAIN PROGRAM============#
- #=====================================#
- def main():
- menu(menus)
- choice = input("Please make a selection(1-3): ")
- #IP
- if choice == 1:
- ip = raw_input("Please enter an IP to scan: ")
- scanner(ip, line)
- elapsed(start, end)
- #Hostname
- elif choice == 2:
- parameter = raw_input("Please enter a hostname to scan: ")
- ip = socket.gethostbyname(parameter) #convert hostname to ip
- scanner(ip, line)
- elapsed(start, end)
- #Exit
- elif choice == 3:
- print "Exiting Program. Goodbye"
- return
- #Fault
- else:
- print "Invalid Selection"
- return main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement