-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacler.rb
executable file
·104 lines (84 loc) · 1.86 KB
/
acler.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
# A script to test ACL on a connection between networks
require 'timeout'
require 'threadify'
require 'socket'
Signal.trap("INT") {
shut_down
exit
}
inuse = []
class Port
@@array = []
attr_accessor :port, :state, :reason
def initialize(port, state, reason)
@port = port
@state = state
@reason = reason
@@array << self
end
def self.all
@@array
end
end
def shut_down
puts "\nShutting down..."
exit
end
options = '
___ ________ __________
/ | / ____/ / / ____/ __ \
/ /| |/ / / / / __/ / /_/ /
/ ___ / /___/ /___/ /___/ _, _/
/_/ |_\____/_____/_____/_/ |_|
server - spawn a server and wait for connection from client
client - spawn a client to connect to server with
E.G.
./acler.rb server
./acler.rb client 10.0.0.1
'
if ARGV[0].nil?
puts options
end
if ARGV[0] == "client"
puts "Starting Client, please ensure the server is running first..."
(1..65535).each do |port|
begin
Timeout.timeout(1) do
client = TCPSocket.new ARGV[1], port
got_port = client.gets.chomp
client.close
Port.new(got_port, "OPEN", nil)
print "Connected on port: #{got_port}\r"
$stdout.flush
end
rescue Timeout::Error
Port.new(port, "UNKNOWN", "TIMEOUT")
rescue => error
Port.new(port, "CLOSED", error)
end
end
open("acler_#{Time.now.strftime('%Y-%m-%d_%H-%M')}.txt", "a") do |line|
Port.all.each do |object|
line << "#{object.port} : #{object.state}\n"
end
end
puts "Done"
elsif ARGV[0] == "server"
puts "Starting Server..."
(1..65535).threadify(10) {|port|
begin
server = TCPServer.open(port)
client = server.accept
client.puts port
client.close
print "Got connection on port: #{port}\r"
$stdout.flush
rescue Errno::EADDRINUSE => error
inuse << port
next
rescue => error
error.class.to_s
end
}
end