Advertisement
DOGGYWOOF

Network scanner

Aug 28th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. -- Function to draw the header of Doggy OS Network Controller
  2. function drawHeader()
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. print("====================================")
  6. print(" Doggy OS Network Controller ")
  7. print("====================================")
  8. print("Monitoring network in real-time...")
  9. print("")
  10. end
  11.  
  12. -- Function to automatically detect a modem and open rednet
  13. function openModem()
  14. local modemSide
  15. for _, side in ipairs({"left", "right", "top", "bottom", "front", "back"}) do
  16. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  17. modemSide = side
  18. break
  19. end
  20. end
  21.  
  22. if modemSide then
  23. rednet.open(modemSide)
  24. print("[INFO] Modem found and opened on side: " .. modemSide)
  25. return modemSide
  26. else
  27. return nil
  28. end
  29. end
  30.  
  31. -- Function to display a styled error message
  32. function showError(message)
  33. print("")
  34. print("====================================")
  35. print(" SYSTEM HALTED ")
  36. print("====================================")
  37. print("ERROR: " .. message)
  38. print("====================================")
  39. print("")
  40. end
  41.  
  42. -- Function to continuously monitor connected computers
  43. function monitorConnectedComputers()
  44. drawHeader()
  45.  
  46. local modemSide = openModem() -- Automatically detect and open modem
  47.  
  48. if modemSide then
  49. local knownComputers = {} -- Table to keep track of known computers
  50.  
  51. while true do
  52. local connectedComputers = {} -- Fresh list each cycle
  53. for _, side in ipairs(peripheral.getNames()) do
  54. if peripheral.getType(side) == "computer" then
  55. local computerID = peripheral.call(side, "getID")
  56. local status, response = pcall(peripheral.call, side, "isOn")
  57. local isOn = status and response and "On" or "Off"
  58. connectedComputers[computerID] = {id = computerID, side = side, status = isOn}
  59. end
  60. end
  61.  
  62. term.clear()
  63. drawHeader()
  64.  
  65. if next(connectedComputers) then
  66. print("Connected Computers:")
  67. print("------------------------------------")
  68. for _, computer in pairs(connectedComputers) do
  69. print("ID: " .. computer.id .. " | Side: " .. computer.side .. " | Status: " .. computer.status)
  70. end
  71. print("------------------------------------")
  72. print("[INFO] Monitoring in progress... Press Ctrl+T to stop.")
  73. else
  74. print("[INFO] No connected devices found.")
  75. end
  76.  
  77. -- Update known computers for the next cycle
  78. knownComputers = connectedComputers
  79.  
  80. sleep(5) -- Wait for 5 seconds before refreshing the scan
  81. end
  82.  
  83. rednet.close(modemSide) -- Close the modem when done
  84. else
  85. showError("No Modem Found or Local Area Network Connection Failed.")
  86. handleError("No Modem Found or Local Area Network Connection Failed.")
  87. end
  88. end
  89.  
  90. -- Function to handle errors with user options
  91. function handleError(message)
  92. showError(message)
  93. print("Choose an option:")
  94. print("1. Retry")
  95. print("2. Shut Down")
  96. print("3. Reboot")
  97. print("====================================")
  98. io.write("Enter your choice: ")
  99.  
  100. local choice = read()
  101. if choice == "1" then
  102. monitorConnectedComputers()
  103. elseif choice == "2" then
  104. os.shutdown()
  105. elseif choice == "3" then
  106. os.reboot()
  107. else
  108. print("[ERROR] Invalid choice. Rebooting...")
  109. sleep(2)
  110. os.reboot()
  111. end
  112. end
  113.  
  114. -- Run the function to start real-time monitoring
  115. monitorConnectedComputers()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement