MtnMCG

nettool

Sep 5th, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. -- Network Scanner for Advanced Ender Pocket Computer
  2.  
  3. local modem = peripheral.find("modem") or error("No modem attached", 0)
  4. local activePorts = {}
  5. local currentBatch = 1
  6. local BATCH_SIZE = 100
  7. local MAX_PORT = 65535
  8.  
  9. -- Function to scan a batch of ports
  10. local function scanPortBatch()
  11. local startPort = (currentBatch - 1) * BATCH_SIZE + 1
  12. local endPort = math.min(startPort + BATCH_SIZE - 1, MAX_PORT)
  13.  
  14. -- Close previous batch
  15. if currentBatch > 1 then
  16. for port = startPort - BATCH_SIZE, startPort - 1 do
  17. modem.close(port)
  18. end
  19. end
  20.  
  21. -- Open and scan new batch
  22. for port = startPort, endPort do
  23. modem.open(port)
  24. if modem.isOpen(port) and not activePorts[port] then
  25. activePorts[port] = true
  26. print("New active port detected: " .. port)
  27. elseif not modem.isOpen(port) and activePorts[port] then
  28. activePorts[port] = nil
  29. print("Port closed: " .. port)
  30. end
  31. end
  32.  
  33. -- Move to next batch or restart
  34. currentBatch = currentBatch * BATCH_SIZE >= MAX_PORT and 1 or currentBatch + 1
  35. end
  36.  
  37. -- Function to listen for messages on all ports
  38. local function listenForMessages()
  39. while true do
  40. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  41. term.clear()
  42. term.setCursorPos(1,1)
  43. print("Active Ports: " .. table.concat(getActivePortsList(), ", "))
  44. print("\nLast Message Received:")
  45. print("Port: " .. channel)
  46. print("Message: " .. textutils.serialize(message))
  47. print("Distance: " .. distance)
  48. print("Reply Channel: " .. replyChannel)
  49. print("\nCurrently scanning ports: " .. ((currentBatch - 1) * BATCH_SIZE + 1) .. " - " .. math.min(currentBatch * BATCH_SIZE, MAX_PORT))
  50. print("Press 'q' to exit")
  51. end
  52. end
  53.  
  54. -- Function to get a list of active ports
  55. local function getActivePortsList()
  56. local portList = {}
  57. for port in pairs(activePorts) do
  58. table.insert(portList, port)
  59. end
  60. table.sort(portList)
  61. return portList
  62. end
  63.  
  64. -- Function to check for 'q' key press
  65. local function checkForExit()
  66. while true do
  67. local event, key = os.pullEvent("key")
  68. if key == keys.q then
  69. return true
  70. end
  71. end
  72. end
  73.  
  74. -- Main function
  75. local function main()
  76. term.clear()
  77. term.setCursorPos(1,1)
  78. print("Network Scanner")
  79. print("Scanning ports in batches...")
  80. print("Press 'q' to exit")
  81.  
  82. -- Start scanning and listening in separate coroutines
  83. parallel.waitForAny(
  84. function()
  85. while true do
  86. scanPortBatch()
  87. os.sleep(0.1) -- Scan every 0.1 seconds for faster cycling
  88. end
  89. end,
  90. listenForMessages,
  91. checkForExit
  92. )
  93.  
  94. -- Close all ports before exiting
  95. for port = 1, MAX_PORT do
  96. modem.close(port)
  97. end
  98.  
  99. print("Exiting...")
  100. end
  101.  
  102. -- Run the main function
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment