Day 1: Port Banner Grabber (Python)

in Python5 days ago

Banner Grabber collects information about computer system on a network or service running on a its open ports. The python code for this service is given below:


LEARN.png

Customiezed on Canva

To collect information about a system we need a python library **socket**. > import socket

The open ports we want to see.

COMMON_PORTS = [22, 80, 443]

Then a function to do our job:

def grab_banner(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPv4 TCP socket
sock.settimeout(5) # Set a 5-second timeout
sock.connect((ip, port))
try:
banner = sock.recv(1024).decode().strip()
print(f"[+] Port {port} Banner: {banner}")
except:
print(f"[+] Port {port} is open but no banner received")
sock.close()
except Exception as e:
print(f"[-] Port {port} is closed or filtered ({e})")

To run the grab_banner function, we have created a main function, because all other languages contain a main function.

if name == "main":
ip = input("Enter target IP or hostname: ").strip()
print(f"\nScanning {ip}...\n")
for port in COMMON_PORTS:
grab_banner(ip, port)

Use command to run program:

python3 program_name.py

image.png

Sort:  

Congratulations @hangerio! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You received more than 4000 upvotes.
Your next target is to reach 4250 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Hive Power Up Day - October 1st 2025