simple portscanner in python
How to make a simple port scanner in python:
okay first we need to understand what a portscanner does, basically it is a programming which normally by trial and error checks which ports are open on a said computer.
To make a portscanner we need to understand what our program must do. Well basically it should request an IP to scan and the ports to scan( this while be quite slow at scanning so it is preferable if it was as selective as needed) then it must attempt to cnnect in turn to these ports and some how out put the status.( I use a log file as it is more usfull than an OSD and it would be too slow to use both.
okay so what we need to learn about is how to: -capture keybord input.
-probably how to make a while clause
-connect to a port
easy enough.so please learn about sockets, raw_input while and basically the basics of python before trying my code :D well heres my code:
#!/python24 #location of my python install
import socket #we need to import the socket function to connect
host = raw_input("What is the IP? ") # gather needed info
port0 = raw_input("What is the starting port? ")
port1 = raw_input("What is the finishing port? ")
output = raw_input("What shall the log output file be called? ")
port = port0 # make sure it starts at start port
print "Scanning ports..."#astetics are always nice and if your program malfuctuins its easier to locate
z = open(output + ".txt", "a")#opening the file we need we do this once and before thewhile so its faster
z.write("Scan result: ")
while int(port) <= int(port1):# tell it when to stop. and use the interger bit int() because else it wont use numbers
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#open the socket and connect
s.connect((host, int(port)))
z.write("Port: " + str(port) + " is open. ")#if it connects it's open ;)
s.close()
print port
port = int(port) + 1 #move on to next port :D
except socket.error:# if the socket doesnt work then it's closed ;)
z.write("Port: " + str(port) + " is closed. ")
s.close()
print port
port = int(port) + 1
#nothng so the program will exit
#the author me,(wolfmankurd) didnt make you do anything so no law suits please :D ToS style use at your own risk.
okay first we need to understand what a portscanner does, basically it is a programming which normally by trial and error checks which ports are open on a said computer.
To make a portscanner we need to understand what our program must do. Well basically it should request an IP to scan and the ports to scan( this while be quite slow at scanning so it is preferable if it was as selective as needed) then it must attempt to cnnect in turn to these ports and some how out put the status.( I use a log file as it is more usfull than an OSD and it would be too slow to use both.
okay so what we need to learn about is how to: -capture keybord input.
-probably how to make a while clause
-connect to a port
easy enough.so please learn about sockets, raw_input while and basically the basics of python before trying my code :D well heres my code:
#!/python24 #location of my python install
import socket #we need to import the socket function to connect
host = raw_input("What is the IP? ") # gather needed info
port0 = raw_input("What is the starting port? ")
port1 = raw_input("What is the finishing port? ")
output = raw_input("What shall the log output file be called? ")
port = port0 # make sure it starts at start port
print "Scanning ports..."#astetics are always nice and if your program malfuctuins its easier to locate
z = open(output + ".txt", "a")#opening the file we need we do this once and before thewhile so its faster
z.write("Scan result: ")
while int(port) <= int(port1):# tell it when to stop. and use the interger bit int() because else it wont use numbers
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#open the socket and connect
s.connect((host, int(port)))
z.write("Port: " + str(port) + " is open. ")#if it connects it's open ;)
s.close()
print port
port = int(port) + 1 #move on to next port :D
except socket.error:# if the socket doesnt work then it's closed ;)
z.write("Port: " + str(port) + " is closed. ")
s.close()
print port
port = int(port) + 1
#nothng so the program will exit
#the author me,(wolfmankurd) didnt make you do anything so no law suits please :D ToS style use at your own risk.

Main:
Posted by 
.