Advertisement
DOGGYWOOF

Untitled

Aug 28th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 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 control a device by ID
  43. function controlDevice(deviceID)
  44. print("")
  45. print("Remote Control Device: " .. deviceID)
  46. print("Choose an action:")
  47. print("1. Shutdown")
  48. print("2. Reboot")
  49. print("3. Logoff")
  50. print("====================================")
  51. io.write("Enter your choice: ")
  52.  
  53. local choice = read()
  54. local command = ""
  55.  
  56. if choice == "1" then
  57. command = "/disk/ACPI/shutdown"
  58. elseif choice == "2" then
  59. command = "/disk/ACPI/reboot"
  60. elseif choice == "3" then
  61. command = "/disk/ACPI/logoff"
  62. else
  63. print("[ERROR] Invalid choice.")
  64. return
  65. end
  66.  
  67. -- Send the command to the device
  68. rednet.send(deviceID, command, "control")
  69. print("[INFO] Command sent: " .. command)
  70. end
  71.  
  72. -- Function to continuously monitor connected computers
  73. function monitorConnectedComputers()
  74. drawHeader()
  75.  
  76. local modemSide = openModem() -- Automatically detect and open modem
  77.  
  78. if modemSide then
  79. local knownComputers = {} -- Table to keep track of known computers
  80.  
  81. while true do
  82. local connectedComputers = {} -- Fresh list each cycle
  83. for _, side in ipairs(peripheral.getNames()) do
  84. if peripheral.getType(side) == "computer" then
  85. local computerID = peripheral.call(side, "getID")
  86. local status, response = pcall(peripheral.call, side, "isOn")
  87. local isOn = status and response and "On" or "Off"
  88. connectedComputers[computerID] = {id = computerID, side = side, status = isOn}
  89. end
  90. end
  91.  
  92. term.clear()
  93. drawHeader()
  94.  
  95. if next(connectedComputers) then
  96. print("Connected Computers:")
  97. print("------------------------------------")
  98. for _, computer in pairs(connectedComputers) do
  99. print("ID: " .. computer.id .. " | Side: " .. computer.side .. " | Status: " .. computer.status)
  100. end
  101. print("------------------------------------")
  102. print("[INFO] Monitoring in progress... Press Ctrl+T to stop.")
  103. print("Enter device ID to control (or press Enter to refresh):")
  104.  
  105. io.write("Device ID: ")
  106. local inputID = read()
  107.  
  108. if inputID and inputID ~= "" then
  109. local deviceID = tonumber(inputID)
  110. if connectedComputers[deviceID] then
  111. controlDevice(deviceID)
  112. else
  113. print("[ERROR] Device ID not found.")
  114. end
  115. end
  116.  
  117. else
  118. print("[INFO] No connected devices found.")
  119. end
  120.  
  121. -- Update known computers for the next cycle
  122. knownComputers = connectedComputers
  123.  
  124. sleep(5) -- Wait for 5 seconds before refreshing the scan
  125. end
  126.  
  127. rednet.close(modemSide) -- Close the modem when done
  128. else
  129. showError("No Modem Found or Local Area Network Connection Failed.")
  130. handleError("No Modem Found or Local Area Network Connection Failed.")
  131. end
  132. end
  133.  
  134. -- Function to handle errors with user options
  135. function handleError(message)
  136. showError(message)
  137. print("Choose an option:")
  138. print("1. Retry")
  139. print("2. Shut Down")
  140. print("3. Reboot")
  141. print("====================================")
  142. io.write("Enter your choice: ")
  143.  
  144. local choice = read()
  145. if choice == "1" then
  146. monitorConnectedComputers()
  147. elseif choice == "2" then
  148. os.shutdown()
  149. elseif choice == "3" then
  150. os.reboot()
  151. else
  152. print("[ERROR] Invalid choice. Rebooting...")
  153. sleep(2)
  154. os.reboot()
  155. end
  156. end
  157.  
  158. -- Run the function to start real-time monitoring
  159. monitorConnectedComputers()
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement