Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- if modem == nil then
- term.clear()
- print("Please attach a wireless modem to use the ccw.")
- while true do
- sleep(1)
- end
- end
- function install(programName, programCode)
- local h = fs.open("startup","w")
- h.writeLine("shell.run(\"delete "..programName.."\")")
- h.writeLine("shell.run(\"pastebin get "..programCode.." "..programName.."\")")
- h.writeLine("shell.run(\""..programName.."\")")
- h.close()
- end
- function dhcpInstaller()
- install("DHCP","3bgBhh1Y")
- end
- function routerInstaller()
- install("router","AWeF7DZP")
- end
- function clientInstaller()
- getIP()
- install("client","X0sW3SHg")
- end
- function webserverInstaller()
- getIP()
- install("server","VQ2mnKHc")
- end
- -------------------------------------------------------------------------------------
- function getIP()
- local label = os.getComputerLabel();
- function send(channel, replychannel, table)
- modem.transmit(channel, replychannel, textutils.serialize(table))
- end
- function deserialize(message)
- return textutils.unserialize(message)
- end
- local function getMinecraftTimestamp()
- return string.format("%d:%d:%d", os.day(), os.time(), os.clock())
- end
- if label == nil or label == "" then
- label = getMinecraftTimestamp()
- os.setComputerLabel(label)
- end
- local function setupEventHandler()
- local _, _, senderChannel, _, message, _ = os.pullEvent("modem_message")
- local table
- print("-- Received Event --")
- print("SenderChannel: "..(senderChannel or "nil"))
- print("Message: "..(message or "nil"))
- if pcall(function() table = deserialize(message) end) and table.id == label and table["status"] == "reply" then
- local ip = table["ip"]
- os.setComputerLabel(ip.."")
- end
- end
- local function timeout()
- sleep(5)
- end
- local function setupChannel()
- local attempt = 0
- modem.open(31254)
- while os.getComputerLabel() == label do
- attempt = attempt+1
- term.clear()
- term.setCursorPos(1,1)
- print("Setting up communication channel.")
- print("Attempt #"..attempt)
- local toSend = {["id"]=label}
- send(31255,31254,toSend)
- parallel.waitForAny(timeout, setupEventHandler)
- sleep(0.1)
- end
- modem.close(31254)
- print("Received ip: "..os.getComputerLabel())
- end
- setupChannel()
- end
- -------------------------------------------------------------------------------------
- local label = (os.getComputerLabel() or "")
- if pocket then
- clientInstaller();
- elseif label == "DHCP" then
- dhcpInstaller()
- elseif label == "router" then
- routerInstaller()
- else
- term.clear()
- term.setCursorPos(1,1)
- while true do
- print("Please enter the type you want to configure this device as")
- local input = string.lower(read())
- if input == "dhcp" then
- os.setComputerLabel("DHCP")
- dhcpInstaller()
- break
- elseif input == "router" then
- os.setComputerLabel("router")
- routerInstaller()
- break
- elseif input == "client" then
- clientInstaller()
- break
- elseif input == "server" or input == "webserver" then
- webserverInstaller()
- break
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Invalid input. Type 'Client' to setup a client")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement