-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rb
59 lines (52 loc) · 1.36 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'optparse'
require 'socket'
# Default server and port options
options = {
:ip => 'seashells.io',
:port => '1337'
}
OptionParser.new do |opts|
opts.banner = "Usage: seashells [options]"
opts.on("-i", "--ip [IP | URI]", "custom IP address or URI; default: seashells.io") do |ip|
options[:ip] = ip
end
opts.on("-p", "--port [Port]", Integer, "port for the IP; default: 1337") do |port|
options[:port] = port
end
opts.on("-d", "--delay [Delay Time]", Integer, "Time delay for piping output in seconds") do |delay|
options[:delay] = delay
end
opts.on("-o", "--output", "output to both console and server or just server") do |output|
options[:output] = output
end
end.parse!
def pipe(options={})
begin
socket = TCPSocket.open(options[:ip], options[:port])
rescue Errno::ETIMEDOUT
return "Connection timed out"
rescue Errno::ECONNREFUSED
return "Connection refused"
rescue SocketError
return "Socket Error"
else
puts "Starting the Client...................\n"
seashells_url = socket.gets
if options.has_key? :delay
sleep(options[:delay])
end
console_input = STDIN.read.split("\n")
console_input.each do |a|
socket.write(a)
end
if options.has_key? :output
puts console_input
end
ensure
if defined? seashells_url
puts "Piped output is available at url below", seashells_url
end
socket.close
end
end
pipe(options)