Advertisement
Guest User

oc netcat over modems

a guest
Jul 17th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. local shell = require("shell")
  2. local fs = require("filesystem")
  3. local component = require("component")
  4. local event = require("event")
  5.  
  6. if not component.modem then
  7. error("wireless network card required")
  8. end
  9.  
  10. local modem = component.modem
  11. if not modem.isWireless() then
  12. error("wireless network card required")
  13. end
  14.  
  15. local args, options = shell.parse(...)
  16. if #args < 2 then
  17. error("usage: netcat [-r: receive] <port> <file [use '-' for stdin / recv end stdout]>")
  18. end
  19.  
  20. local port = tonumber(args[1])
  21.  
  22. if not port then
  23. error("invalid port")
  24. end
  25. local file = args[2]
  26.  
  27. if options.r then
  28. modem.open(port)
  29.  
  30. local input_method, input_param = "write", require("tty").getViewport()
  31.  
  32. --for i = 1, #args do
  33. local arg = shell.resolve(args[2])
  34. if fs.isDirectory(arg) then
  35. io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
  36. os.exit(1)
  37. else
  38. local file, reason
  39. if args[2] == "-" then
  40. file, reason = io.stdout, "missing stdout"
  41. input_method, input_param = "write", false
  42. else
  43. file, reason = fs.open(arg)
  44. end
  45. if not file then
  46. io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
  47. os.exit(1)
  48. else
  49. while true do
  50. local _, _, _, _, _, msg = event.pull("modem_message")
  51. if msg ~= "EOF" then
  52. file[input_method](file, msg)
  53. else
  54. print("file received")
  55. break
  56. end
  57. end
  58. file:close()
  59. end
  60. end
  61. --end
  62. else
  63. local input_method, input_param = "read", require("tty").getViewport()
  64.  
  65. --for i = 1, #args do
  66. local arg = shell.resolve(args[2])
  67. if fs.isDirectory(arg) then
  68. io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
  69. os.exit(1)
  70. else
  71. local file, reason
  72. if args[2] == "-" then
  73. file, reason = io.stdin, "missing stdin"
  74. input_method, input_param = "readLine", false
  75. else
  76. file, reason = fs.open(arg)
  77. end
  78. if not file then
  79. io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
  80. os.exit(1)
  81. else
  82. repeat
  83. local chunk = file[input_method](file, input_param)
  84. if chunk then
  85. --io.write(chunk)
  86. modem.broadcast(port, chunk)
  87. end
  88. until not chunk
  89. modem.broadcast(port, "EOF")
  90. print("file sent")
  91. file:close()
  92. end
  93. end
  94. --end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement