Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uns=require("serialization").unserialize
- function use()
- print("Использование:")
- print("'ftp /scan' - сканирует сеть на наличие ftp-серверов")
- print("'ftp <server_name> [<password>]' - подключает ftp-сервер")
- end
- args={...}
- if #args==0 then use() return end
- local comp = require('component')
- local event = require('event')
- if not comp.isAvailable("modem") then
- print("Сетевая карта не найдена")
- return
- end
- local modem = comp.modem
- local FTPport = 21
- modem.open(FTPport)
- if args[1]=="/scan" then
- print("Сканирование ftp-серверов")
- modem.broadcast(FTPport,"getLabel")
- local found=false
- repeat
- local eventname, _,sendID, _, dist, comm, name = event.pull(3,"modem_message")
- if eventname then print(name, "dist="..dist, "id="..sendID) found = true end
- until eventname==nil
- if not found then print("Доступные ftp-сервера не найдены") end
- return
- end
- function getFS(addr,name)
- local function exec(command, ...)
- modem.send(addr,FTPport,command, ... )
- while true do
- local eventname, _,sendID, _, _, comm, result = event.pull(3,"modem_message")
- if not eventname then return nil, "Timeout FTP-request" end
- if eventname=="modem_message" and sendID==addr and comm==command then
- return result
- end
- end
- end
- return{
- type="filesystem",address=addr, slot=0,
- isReadOnly=function() return exec("isReadOnly") end,
- spaceTotal=function() return exec("spaceTotal") end,
- spaceUsed=function() return exec("spaceUsed") end,
- getLabel=function() return exec("getLabel") end,
- makeDirectory=function(path)
- checkArg(1, path, "string")
- return exec("makeDirectory",path)
- end,
- isDirectory=function(path)
- checkArg(1, path, "string")
- return exec("isDirectory",path)
- end,
- exists=function(path)
- checkArg(1, path, "string")
- return exec("exists",path)
- end,
- remove=function(path)
- checkArg(1, path, "string")
- return exec("remove",path)
- end,
- lastModified=function(path)
- checkArg(1, path, "string")
- return exec("lastModified",path)
- end,
- size=function(path)
- checkArg(1, path, "string")
- return exec("size",path)
- end,
- open=function(path, mode)
- checkArg(1, path, "string")
- mode = tostring(mode or "r")
- checkArg(2, mode, "string")
- return exec("open",path,mode)
- end,
- rename=function(from, to)
- checkArg(1, from, "string")
- checkArg(2, to, "string")
- return exec("rename",from,to)
- end,
- list=function(path)
- checkArg(1, path, "string")
- return uns(exec("list",path))
- end,
- setLabel=function(value) return name end, --:string
- write=function(handle, value)
- checkArg(1, handle, "number")
- checkArg(2, value, "string")
- repeat
- if not exec("write",handle,value:sub(1,8000)) then return false end
- value=value:sub(8001)
- until #value==0
- return true
- end,
- read=function(handle, count)
- checkArg(1, handle, "number")
- checkArg(2, count, "number")
- return exec("read",handle, count)
- end,
- seek=function(handle, whence, offset)
- checkArg(1, handle, "number")
- checkArg(2, whence, "string")
- checkArg(3, offset, "number")
- return exec("seek",handle, whence, offset)
- end,
- close=function(handle)
- checkArg(1, handle, "number")
- return exec("close",handle)
- end,
- }
- end
- nameNode=args[1]
- modem.broadcast(FTPport,"getLabel")
- while true do
- local eventname, _,sendID, _, _, comm, name = event.pull(3,"modem_message")
- if eventname and name==nameNode then
- require("filesystem").mount(getFS(sendID,nameNode), "/ftp/"..nameNode)
- return
- end
- if not eventname then print("ftp-сервер не найден") return end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement