Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local shell = require("shell")
- local fs = require("filesystem")
- local component = require("component")
- local event = require("event")
- if not component.modem then
- error("network card required")
- end
- local modem = component.modem
- local args, options = shell.parse(...)
- if #args < 2 then
- error("usage: netcat [-r: receive] <port> <file [use '-' for stdin / recv end stdout]>")
- end
- local port = tonumber(args[1])
- if not port then
- error("invalid port")
- end
- local file = args[2]
- if options.r then
- modem.open(port)
- local input_method, input_param = "write", require("tty").getViewport()
- --for i = 1, #args do
- local arg = shell.resolve(args[2])
- if fs.isDirectory(arg) then
- io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
- os.exit(1)
- else
- local file, reason
- if args[2] == "-" then
- file, reason = io.stdout, "missing stdout"
- input_method, input_param = "write", false
- else
- file, reason = fs.open(arg, "w")
- end
- if not file then
- io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
- os.exit(1)
- else
- while true do
- local _, _, _, _, _, msg = event.pull("modem_message")
- if msg ~= "EOF" then
- file[input_method](file, msg)
- else
- print("file received")
- break
- end
- end
- file:close()
- end
- end
- --end
- else
- local input_method, input_param = "read", require("tty").getViewport()
- --for i = 1, #args do
- local arg = shell.resolve(args[2])
- if fs.isDirectory(arg) then
- io.stderr:write(string.format('netcat %s: Is a directory\n', arg))
- os.exit(1)
- else
- local file, reason
- if args[2] == "-" then
- file, reason = io.stdin, "missing stdin"
- input_method, input_param = "readLine", false
- else
- file, reason = fs.open(arg)
- end
- if not file then
- io.stderr:write(string.format("netcat: %s: %s\n", args[i], tostring(reason)))
- os.exit(1)
- else
- repeat
- local chunk = file[input_method](file, input_param)
- if chunk then
- --io.write(chunk)
- modem.broadcast(port, chunk)
- end
- until not chunk
- modem.broadcast(port, "EOF")
- print("file sent")
- file:close()
- end
- end
- --end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement