Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Network Scanner for Advanced Ender Pocket Computer
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- local activePorts = {}
- local currentBatch = 1
- local BATCH_SIZE = 100
- local MAX_PORT = 65535
- -- Function to scan a batch of ports
- local function scanPortBatch()
- local startPort = (currentBatch - 1) * BATCH_SIZE + 1
- local endPort = math.min(startPort + BATCH_SIZE - 1, MAX_PORT)
- -- Close previous batch
- if currentBatch > 1 then
- for port = startPort - BATCH_SIZE, startPort - 1 do
- modem.close(port)
- end
- end
- -- Open and scan new batch
- for port = startPort, endPort do
- modem.open(port)
- if modem.isOpen(port) and not activePorts[port] then
- activePorts[port] = true
- print("New active port detected: " .. port)
- elseif not modem.isOpen(port) and activePorts[port] then
- activePorts[port] = nil
- print("Port closed: " .. port)
- end
- end
- -- Move to next batch or restart
- currentBatch = currentBatch * BATCH_SIZE >= MAX_PORT and 1 or currentBatch + 1
- end
- -- Function to listen for messages on all ports
- local function listenForMessages()
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- term.clear()
- term.setCursorPos(1,1)
- print("Active Ports: " .. table.concat(getActivePortsList(), ", "))
- print("\nLast Message Received:")
- print("Port: " .. channel)
- print("Message: " .. textutils.serialize(message))
- print("Distance: " .. distance)
- print("Reply Channel: " .. replyChannel)
- print("\nCurrently scanning ports: " .. ((currentBatch - 1) * BATCH_SIZE + 1) .. " - " .. math.min(currentBatch * BATCH_SIZE, MAX_PORT))
- print("Press 'q' to exit")
- end
- end
- -- Function to get a list of active ports
- local function getActivePortsList()
- local portList = {}
- for port in pairs(activePorts) do
- table.insert(portList, port)
- end
- table.sort(portList)
- return portList
- end
- -- Function to check for 'q' key press
- local function checkForExit()
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.q then
- return true
- end
- end
- end
- -- Main function
- local function main()
- term.clear()
- term.setCursorPos(1,1)
- print("Network Scanner")
- print("Scanning ports in batches...")
- print("Press 'q' to exit")
- -- Start scanning and listening in separate coroutines
- parallel.waitForAny(
- function()
- while true do
- scanPortBatch()
- os.sleep(0.1) -- Scan every 0.1 seconds for faster cycling
- end
- end,
- listenForMessages,
- checkForExit
- )
- -- Close all ports before exiting
- for port = 1, MAX_PORT do
- modem.close(port)
- end
- print("Exiting...")
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment