Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Last active March 17, 2026 22:55
Show Gist options
  • Select an option

  • Save MCJack123/216ff534ac18013b6e394ecdd974b83f to your computer and use it in GitHub Desktop.

Select an option

Save MCJack123/216ff534ac18013b6e394ecdd974b83f to your computer and use it in GitHub Desktop.
Port scanner for CC
local ip = ...
if not ip then error("Usage: portscan <ip|domain>") end
term.clear()
term.setCursorPos(1, 1)
print("Scanning", ip, "for open ports")
local nopen, nrecv = 0, 0
local win = window.create(term.current(), 1, 4, term.getSize(), select(2, term.getSize()) - 3)
win.setCursorBlink(true)
local start = os.epoch "utc"
parallel.waitForAll(function()
for i = 1, 65535 do
while i - nrecv > 250 do
repeat local event = os.pullEvent()
until event == "http_success" or event == "http_failure"
end
term.setCursorPos(1, 2)
term.clearLine()
term.write("Send: " .. i)
win.restoreCursor()
local ok1, ok2 = pcall(http.request, {url = "http://" .. ip .. ":" .. i, redirect = false, timeout = 10})
if not (ok1 and ok2) then
repeat local event = os.pullEvent()
until event == "http_success" or event == "http_failure"
end
end
end, function()
while nrecv < 65535 do
local event, url, res, errh = os.pullEvent()
if event == "http_success" then
local port = url:match ":(%d+)"
term.setCursorPos(1, 3)
term.clearLine()
term.write("Recv: " .. port .. " (" .. nrecv .. "/65535)")
local old = term.redirect(win)
term.setTextColor(colors.green)
write(port .. " ")
term.setTextColor(colors.white)
term.redirect(old)
nopen = nopen + 1
nrecv = nrecv + 1
res.close()
elseif event == "http_failure" then
local port = url:match ":(%d+)"
if port then
term.setCursorPos(1, 3)
term.clearLine()
term.write("Recv: " .. port .. " (" .. nrecv .. "/65535)")
if res ~= "Connection refused" and res ~= "Could not connect" then
local old = term.redirect(win)
term.setTextColor(errh and colors.yellow or colors.red)
write(port .. " ")
term.setTextColor(colors.white)
term.redirect(old)
nopen = nopen + 1
else win.restoreCursor() end
if errh then errh.close() end
nrecv = nrecv + 1
end
end
end
end)
print("\nScan completed with", nopen, "open ports in", (os.epoch "utc" - start) / 1000, "seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment