![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Very simple Python port scanner.
Well.. a friend asked for help with a Python port scanner he was working on, and I tweaked it and came up with this. I tried commenting the semi-confusing or interesting parts so you socket-noobs can understand it (I say "tried," because I'm not very good at explaining myself.
) Anyway, do you have any ideas as to what I could do to make it better? Thanks in advance.import socket
target= raw_input("Target: ")
startport= input("Starting port: ")
endport= input("Ending port: ")
for port in range(startport,endport + 1):
#I have '+ 1' because range() ends one number under the second argument.
scan = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scan.settimeout(1)
if not scan.connect_ex((target, port)):
#connect_ex would return a number higher than zero had it failed, so 'not' is appropriate.
print "Port",port,"is open."
scan.close() |
|
|
|
|
|
#2 |
|
Programmer
|
Okay I've updated it, with a logging function.. now it appends all open ports to a file. This is the code to do that:
log= file("scan_log.log","a")
log.write("Open ports for %s:\n" % target)
#the next line should be within the for loop. I had to use %s because write() wanted a string.. and port was (obviously) an int.
log.write("%s\n" % port)
#and this should be outside of the for loop (last line)
log.close() |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() ![]() |
Quote:
log.write(str(port)+'\n') =S Edit: Oh were you saying you had to use %s as opposed to %d? |
|
|
|
|
|
|
#4 |
|
Programmer
|
I hadn't thought of that.
![]() Yes I was saying I had to use %s instead of %d. Had I been using printf() in C++, I would have used %d... but I'm not sure how much of a difference it makes in Python. Either way, it works. |
|
|
|
|
|
#5 | ||
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|