Open Port Checks OneLiner

Test a TCP (or UDP) remote port in one-line

timeout 1 bash -c '</dev/tcp/216.58.207.46/443 && echo Port is open || echo Port is closed' || echo Connection timeout

Fedora:
ss 216.58.207.46/443


curl telnet://127.0.0.1:22

curl -s 216.58.207.46:443 >/dev/null && echo Port is open || echo Port is closed.
.. || One’s inside the quotes, and one’s outside
.. The inside one ORs off the socket opening, the outside one off the bash execution.


cat < /dev/tcp/127.0.0.1/22


python
>>> import socket
>>> socket.create_connection(address=('216.58.207.46',443),timeout=5)


Netcat:
nc -zv 127.0.0.1 80
nc -zv 127.0.0.1 22 80 8080
nc -zv 127.0.0.1 20-30
nc -w1 127.0.0.1 22 </dev/null
..the -w flag takes care of the timeout, and the </dev/null replaces the -z flag


(echo > /dev/tcp/skinner/22) >/dev/null 2>&1 && echo "It's up" || echo "It's down"
(echo > /dev/udp/skinner/222) >/dev/null 2>&1 && echo "It's up" || echo "It's down"


telnet 192.168.5.5 25
telnet www.example.net 80

Python Port Scan

import socket
from colorama import init, Fore

init()
GREEN = Fore.GREEN
RESET = Fore.RESET
GRAY = Fore.LIGHTBLACK_EX

host = input("Enter Host:")

def is_port_open(host, port):
    s = socket.socket()
    try:
        s.connect((host, port))
        s.settimeout(0.2)
    except:
        return False  #port is closed
    else:
        return True   #port is open

for port in range(1, 1025):
    if is_port_open(host, port):
        print(f"{GREEN}[+] {host}:{port} is open      {RESET}")
    else:
        print(f"{GRAY}[!] {host}:{port} is closed    {RESET}", end="\r")

Last updated