Arslan avatar

Day 1: Port Banner Grabber (Python)

hangerio

Published: 27 Sept 2025 › Updated: 27 Sept 2025Day 1: Port Banner Grabber (Python)

Day 1: Port Banner Grabber (Python)

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

Leave Day 1: Port Banner Grabber (Python) to:

Written by

I'm a cyber learner

Read more # posts


Best Posts From Arslan

We have not curated any of hangerio's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Arslan