Windows

ChatGPT one liner Powershell command:
$h="x.x.x.x";1..65535|%{try{$c=New-Object Net.Sockets.TcpClient;$r=$c.BeginConnect($h,$_,$null,$null);if($r.AsyncWaitHandle.WaitOne(100)-and$c.Connected){echo "port $_ open"};$c.Close()}catch{}}

ChatGPT Powershell script:

$host = "x.x.x.x"; 1..65535 | ForEach-Object { 
    try { 
        $client = New-Object System.Net.Sockets.TcpClient; 
        $async = $client.BeginConnect($host, $_, $null, $null); 
        $success = $async.AsyncWaitHandle.WaitOne(100); 
        if ($success -and $client.Connected) {
            Write-Host "Port $_ is open"
            $client.Close()
        }
    } catch {} 
}

Linux

ChatGPT improved one liner:
host=x.x.x.x; for port in {1..65535}; do timeout 0.1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null && echo "port $port is open"; done

ChatGPT improved parallel:
host=x.x.x.x; for port in {1..65535}; do (timeout 0.1 bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null && echo "port $port is open") & done; wait

Original:

#!/bin/bash
host=x.x.x.x
for port in {1..65535}; do
    timeout .1 bash -c "echo >/dev/tcp/$host/$port" &&
        echo "port $port is open"
done
echo "Done"