An easy to use Python package that could perform port scanning conveniently.
An output example is showed as following:
- Use
git clone https://github.com/YaokaiYang-assaultmaster/PythonPortScanner.git
command to clone the repository to your own machine. - There is 2 methods for you to use this module.
- One is to put
PortScanner package
in your working directory and importPortScanner
to your code. In this way you can only use it in your current working directory. - The other is to execute
python setup.py install
in thePortScanner
package. In this way it will be installed to your python'ssite-packages
folder. And after that you can use it globally byimport PortScanner
.
- One is to put
- Voilà ! You are ready to go!
- Add
import PortScanner
orfrom PortScanner import PortScanner
in your code. - Initialize a new PortScanner object using
scanner = PortScanner.PortScanner()
orscanner = PortScanner()
. You could also put the list of ports you want to scan (if any) as a pythonlist
and pass it as thetarget_ports
argument to the constructor. - Then call
scanner.scan(host_name)
to perform scan task. - Note that the total scan time for a target website is highly related to the timeout value (delay) set for the Scanner object. Thus for the seek of efficiency, the timeout should not be too long.
-
Constructor
__init__(self, target_ports=None)
takes a list of ports or an int as the argument. If not provided, it will perform scanning task using default ports. If a list is provided, the list will be used as the list of ports being scanned. If a int is provided (this int need to be 50, 100 or 1000), the top 50, 100 or 1000 commonly used ports will be used. -
Functions
scanner.scan(host_name, message = '')
is the function need to be called to perform port scanning. It takes 2 arguments.host_name
is the hostname that is going to be scannedmessage
is the message that is going to be included in the scanning packets sent out. This is provided in order to prevent ethical problem. If not provided, no message will be included in the packets.
scanner.set_thread_limit(limit)
is the function to set the maximum number of threads run concurrently for port scanning. It takes 1 argument.limit
is the maximum number of threads allowed. The valid limit range is 1 to 50,000. The default value is 1,000.
scanner.set_delay(delay)
is the function to set the timeout delay for port scanning in seconds. It takes 1 argument.delay
the time in seconds that a TCP socket waits until timeout. The valid delay range is 1s to 100s. The default value is 10s.
scanner.show_target_ports()
is used to get the list of ports being scanned for current Scanner object.scanner.show_delay()
is used to get current timeout interval in seconds that a TCP socket waits.scanner.show_top_k_ports(k)
is used to get top 50, top 100 or top 1000 port lists. Other k will raise anValueError
-
An example usage case is showed in
PortScanner/PortScanExample.py