Port Scanner in Python 3 (pyDroid and iPhone)
Heyyy! How ya doing?
Finally I have some free time and able to make some more videos! Very excited to learn some more stuff and share it with you guys. 🙂
To start things off smoothly I decided to do a short Python script – a port scanner, which we’ll program in a few lines of code.
This is an idea suggested by a viewer, to do small projects using mobile devices (pyDroid on Android and vim text editor on jailbroken iPhone).
Once you figure out which device you’ll be using, lets get started…
Port Scanner Using Threads
It’s important to use threads for such a program to shorten run time. With that said, we’ll need the system, socket and threading modules.
import socket,sys,threading print('\n> Python3 Port Scanner\n '+'='*20+'\n') if len(sys.argv) > 1: target = socket.gethostbyname(sys.argv) else: target = socket.gethostbyname(input('> Enter target: ')) # Scan ports - arguments: target + port def scanPort(target, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c = s.connect_ex((target, port)) if c == 0: print (' ' + target + ' ~ Port %d \t[open]' % (port,)) s.close() # Here we define range of ports to scan for i in range(1, 1024): scan = threading.Thread(target=scanPort, args=(target, i)) scan.start()
On the second line we’ll simply greet the user.
Line 3 and 4 we’ll check for arguments (target IP address).
Lines 5 thru 10 we’ll create a function to scan target on specific port. This will be the threaded function – once we find an open port we’ll print ‘open’.
The last 4 lines of code we’ll iterate through our desired port range (1 to 1024 – feel free to change it), creating a thread for each one of them.
I just wanted to stop by and do a quick warm up for what’s to come.
Hope you guys enjoyed this short tutorial and see ya next time!
[…] this post we’re going to take some code from a previous post ‘Port Scanner in Python 3‘ and converting it into a program with a graphical interface that you could run from a mobile […]