What is Open Port?
Ports are open and closed. If a port is not open, we won’t be able to make a connection between two systems. So, when we do any activity on the network, the required ports are open and, as a result, we get the response in our system. Now, after discussing these three keywords, you will be able to understand what we are actually trying to do. To get an open port banner we will use a socket module. The socket is a way of connecting two nodes on a network to communicate with each other. One node listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.
Example 1
First, let’s see how we can see which ports are open for our system for that we are using threading for fast computation. We can get the IP address of the localhost by passing the host variable to the gethostbyname() function of the socket module. Here AF_INET specifies the IP address is IPV4(Internet Protocol version 4) and SOCKET_STREAM specifies it’s a TCP socket. Next, we have set the status=False, which will be true whenever we make a connection. connect() function is used to make connections between host_ip and port.
Python3
import socket import threading import time # function to scan ports and see which ports are open def scan_port(port): # we will check port of localhost host = "localhost" host_ip = socket.gethostbyname(host) # print("host_ip = {}".format(host_ip)) status = False # create instance of socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connecting the host ip address and port try : s.connect((host_ip, port)) status = True except : status = False if status: print ( "port {} is open" . format (port)) start_time = time.time() for i in range ( 0 , 100000 ): thread = threading.Thread(target = scan_port, args = [i]) thread.start() end_time = time.time() print ( "To all scan all ports it took {} seconds" . format (end_time - start_time)) |
Example 2
Here you can see we have added s.recv(1024).decode(). This means the socket will return the banner in 1024 bytes of buffer size and then we decode it to a string. Now to get banners on these open ports, we need to add just one more line after making the connection, banner = s.recv(1024).decode().
Python3
import socket import threading import time def scan_port(port): try : host = "localhost" host_ip = socket.gethostbyname(host) status = False # create instance of socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connecting the host ip address and port s.connect((host_ip, port)) try : banner = s.recv( 1024 ).decode() print ( "port {} is open with banner {}" . format (port, banner)) except : print ( "port {} is open " . format (port)) except : pass start_time = time.time() for i in range ( 0 , 100000 ): thread = threading.Thread(target = scan_port, args = [i]) thread.start() end_time = time.time() print ( "To scan all ports it took {} seconds" . format (end_time - start_time)) |
Output:
How to Get Open Port Banner in Python
In this article, we will see how to Get Open Port Banner in Python. Here we will discuss
Contact Us