Advertisement
ZNZNCOOP

ftp client

Dec 22nd, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. uns=require("serialization").unserialize
  2.  
  3. function use()
  4.   print("Использование:")
  5.   print("'ftp /scan' - сканирует сеть на наличие ftp-серверов")
  6.   print("'ftp <server_name> [<password>]' - подключает ftp-сервер")
  7. end
  8.  
  9. args={...}
  10. if #args==0 then use() return end
  11.  
  12. local comp   = require('component')
  13. local event  = require('event')
  14.  
  15. if not comp.isAvailable("modem") then
  16.   print("Сетевая карта не найдена")
  17.   return
  18. end
  19. local modem = comp.modem
  20. local FTPport = 21
  21. modem.open(FTPport)
  22.  
  23. if args[1]=="/scan" then
  24.   print("Сканирование ftp-серверов")
  25.   modem.broadcast(FTPport,"getLabel")
  26.   local found=false
  27.   repeat
  28.     local eventname, _,sendID, _, dist, comm, name = event.pull(3,"modem_message")
  29.     if eventname then print(name, "dist="..dist, "id="..sendID) found = true end
  30.   until eventname==nil
  31.   if not found then print("Доступные ftp-сервера не найдены") end
  32.   return
  33. end
  34.  
  35. function getFS(addr,name)
  36.   local function exec(command, ...)
  37.     modem.send(addr,FTPport,command, ... )
  38.     while true do
  39.       local eventname, _,sendID, _, _, comm, result = event.pull(3,"modem_message")
  40.       if not eventname then return nil, "Timeout FTP-request" end
  41.       if eventname=="modem_message" and sendID==addr and comm==command then
  42.        return result
  43.       end
  44.     end
  45.   end
  46.  
  47. return{
  48. type="filesystem",address=addr, slot=0,
  49. isReadOnly=function() return exec("isReadOnly") end,
  50. spaceTotal=function() return exec("spaceTotal") end,
  51. spaceUsed=function() return exec("spaceUsed") end,
  52. getLabel=function() return exec("getLabel") end,
  53.  
  54. makeDirectory=function(path)
  55.   checkArg(1, path, "string")
  56.   return exec("makeDirectory",path)
  57. end,
  58.  
  59. isDirectory=function(path)
  60.   checkArg(1, path, "string")
  61.   return exec("isDirectory",path)
  62. end,
  63.  
  64. exists=function(path)
  65.   checkArg(1, path, "string")
  66.   return exec("exists",path)
  67. end,
  68.  
  69. remove=function(path)
  70.   checkArg(1, path, "string")
  71.   return exec("remove",path)
  72. end,
  73.  
  74. lastModified=function(path)
  75.   checkArg(1, path, "string")
  76.   return exec("lastModified",path)
  77. end,
  78. size=function(path)
  79.   checkArg(1, path, "string")
  80.   return exec("size",path)
  81. end,
  82. open=function(path, mode)
  83.   checkArg(1, path, "string")
  84.   mode = tostring(mode or "r")
  85.   checkArg(2, mode, "string")
  86.   return exec("open",path,mode)
  87. end,
  88.  
  89. rename=function(from, to)
  90.   checkArg(1, from, "string")
  91.   checkArg(2, to, "string")
  92.   return exec("rename",from,to)
  93. end,
  94.  
  95. list=function(path)
  96.   checkArg(1, path, "string")
  97.   return uns(exec("list",path))
  98. end,
  99.  
  100. setLabel=function(value) return name end, --:string
  101.  
  102. write=function(handle, value)
  103.   checkArg(1, handle, "number")
  104.   checkArg(2, value, "string")
  105.   repeat
  106.     if not exec("write",handle,value:sub(1,8000)) then return false end
  107.     value=value:sub(8001)
  108.   until #value==0
  109.   return true
  110. end,
  111.  
  112. read=function(handle, count)
  113.   checkArg(1, handle, "number")
  114.   checkArg(2, count, "number")
  115.   return exec("read",handle, count)
  116. end,
  117.  
  118. seek=function(handle, whence, offset)
  119.   checkArg(1, handle, "number")
  120.   checkArg(2, whence, "string")
  121.   checkArg(3, offset, "number")
  122.   return exec("seek",handle, whence, offset)
  123. end,
  124.  
  125. close=function(handle)
  126.   checkArg(1, handle, "number")
  127.   return exec("close",handle)
  128. end,
  129. }
  130. end
  131.  
  132. nameNode=args[1]
  133. modem.broadcast(FTPport,"getLabel")
  134. while true do
  135.   local eventname, _,sendID, _, _, comm, name = event.pull(3,"modem_message")
  136.   if eventname and name==nameNode then
  137.     require("filesystem").mount(getFS(sendID,nameNode), "/ftp/"..nameNode)
  138.     return
  139.   end
  140.   if not eventname then print("ftp-сервер не найден") return end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement