telnetd 0.0.1.alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -0
- data/Rakefile +8 -0
- data/bin/telnetd +5 -0
- data/lib/telnetd/telnet_client.rb +49 -0
- data/lib/telnetd/telnet_server.rb +65 -0
- data/lib/telnetd.rb +17 -0
- data/make_rdoc.bat +3 -0
- data/telnetd-0.0.1.gem +0 -0
- data/telnetd.gemspec +14 -0
- data/test/test_telnet_server.rb +21 -0
- metadata +56 -0
data/README.md
ADDED
data/Rakefile
ADDED
data/bin/telnetd
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Represent a telnet client.
|
|
2
|
+
# This class is used to be executed in a single thread
|
|
3
|
+
|
|
4
|
+
class TelnetClient
|
|
5
|
+
|
|
6
|
+
# Create a new instance of the telnet client
|
|
7
|
+
# ==== Arguments
|
|
8
|
+
# *+client+ The socket client connection to be wrapped. Must be connected.
|
|
9
|
+
def initialize(client)
|
|
10
|
+
raise ArgumentError, "Client not connected" if client.closed?
|
|
11
|
+
@client = client
|
|
12
|
+
|
|
13
|
+
@port, @ip = Socket.unpack_sockaddr_in(client.getpeername)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Starts the client processing by sending the welcome message.
|
|
17
|
+
# Also enter the processing loop to handle client commands
|
|
18
|
+
def handle()
|
|
19
|
+
welcome_message()
|
|
20
|
+
promt()
|
|
21
|
+
while line = @client.gets
|
|
22
|
+
process_cmd(line)
|
|
23
|
+
promt()
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Close the client connection with sending a bye message and close the socket.
|
|
28
|
+
def close()
|
|
29
|
+
@client.print "\r\nClosing the connection. Bye!"
|
|
30
|
+
@client.close
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def process_cmd(cmd)
|
|
36
|
+
puts cmd
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def promt
|
|
40
|
+
@client.print "\r\n>>"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def welcome_message
|
|
44
|
+
@client.print "Welcome to telnetd.\r\n"
|
|
45
|
+
@client.print "===================\r\n"
|
|
46
|
+
@client.print "Type 'help' to get a list of all commands or 'exit' to end the session.\r\n"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# The Telnet daemon main class. Create a new thread for each client
|
|
2
|
+
|
|
3
|
+
require 'socket'
|
|
4
|
+
require 'telnetd/telnet_client'
|
|
5
|
+
|
|
6
|
+
class TelnetServer
|
|
7
|
+
|
|
8
|
+
# Create a new instance of the Telnet Server class.
|
|
9
|
+
def initialize
|
|
10
|
+
@client_list = []
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Start the server in a new thread. This method will return immediately after the server thread is running.
|
|
14
|
+
# You must use the same instance of the TelnetServer class to stop an existing server by calling the stop
|
|
15
|
+
# method. Otherwise the socket listening thread could not be stopped.
|
|
16
|
+
# * By default the server creates an socket listening on port 23.
|
|
17
|
+
# * Each new connection on this server will be handled by a separate thread.
|
|
18
|
+
def start
|
|
19
|
+
@server_thread = Thread.new do
|
|
20
|
+
begin
|
|
21
|
+
server = TCPServer.open(23)
|
|
22
|
+
loop {
|
|
23
|
+
begin
|
|
24
|
+
client = TelnetClient.new(server.accept)
|
|
25
|
+
@client_list << client
|
|
26
|
+
t = Thread.new do
|
|
27
|
+
handleNewClient(client)
|
|
28
|
+
end
|
|
29
|
+
rescue Exception => e
|
|
30
|
+
puts "Client Error: #{e.message}"
|
|
31
|
+
end
|
|
32
|
+
}
|
|
33
|
+
rescue Interrupt => e
|
|
34
|
+
puts "Server is interrupted"
|
|
35
|
+
ensure
|
|
36
|
+
close_all()
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Used to stop a running server instance.
|
|
42
|
+
# This method will cause all connected clients to be disconnected.
|
|
43
|
+
# ===== Throws
|
|
44
|
+
# * ArgumentError if server is not running or server thread is not alive. Maybe you miss to start the server first.
|
|
45
|
+
def stop
|
|
46
|
+
raise ArgumentError, "Server has not be started." if @server_thread.nil? or @server_thread.alive? == false
|
|
47
|
+
@server_thread.exit
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
def close_all()
|
|
52
|
+
@client_list.each { |client|
|
|
53
|
+
client.close()
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def handleNewClient(client)
|
|
58
|
+
begin
|
|
59
|
+
client.handle()
|
|
60
|
+
rescue Exception => e
|
|
61
|
+
puts "Client Error: #{e.message}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
data/lib/telnetd.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# telnetd console runner
|
|
2
|
+
|
|
3
|
+
require 'telnetd/telnet_server'
|
|
4
|
+
|
|
5
|
+
class Telnetd
|
|
6
|
+
|
|
7
|
+
def self.run
|
|
8
|
+
telnetd = TelnetServer.new
|
|
9
|
+
telnetd.start
|
|
10
|
+
puts "Telnet server started."
|
|
11
|
+
puts "Press any key to stop."
|
|
12
|
+
STDIN.getc
|
|
13
|
+
telnetd.stop
|
|
14
|
+
puts "Telnet server stopped."
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
data/make_rdoc.bat
ADDED
data/telnetd-0.0.1.gem
ADDED
|
Binary file
|
data/telnetd.gemspec
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = 'telnetd'
|
|
5
|
+
spec.version = '0.0.1.alpha.1'
|
|
6
|
+
spec.date = '2013-04-04'
|
|
7
|
+
spec.summary = "Simple telnet daemon"
|
|
8
|
+
spec.description = "Simple telnet daemon to be used from the command line or as embedded service."
|
|
9
|
+
spec.authors = ["Promotos"]
|
|
10
|
+
spec.email = 'promotos@gmx.de'
|
|
11
|
+
spec.files = FileList['lib/**/*.rb', 'bin/*', '[A-Z]*', 'test/**/*'].to_a
|
|
12
|
+
spec.executables << 'telnetd'
|
|
13
|
+
spec.homepage = 'http://rubygems.org/gems/telnetd'
|
|
14
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Tests used for the telnet server
|
|
2
|
+
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
require 'socket'
|
|
5
|
+
require 'telnetd/telnet_server'
|
|
6
|
+
|
|
7
|
+
class TelnetServerTest < Test::Unit::TestCase
|
|
8
|
+
|
|
9
|
+
def test_get_welcome_message
|
|
10
|
+
server = TelnetServer.new
|
|
11
|
+
server.start()
|
|
12
|
+
|
|
13
|
+
s = TCPSocket.open("localhost", 23)
|
|
14
|
+
line = s.gets
|
|
15
|
+
s.close
|
|
16
|
+
|
|
17
|
+
assert_equal("Welcome to telnetd.\r\n", line)
|
|
18
|
+
server.stop()
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: telnetd
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1.alpha.1
|
|
5
|
+
prerelease: 6
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Promotos
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-04-04 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Simple telnet daemon to be used from the command line or as embedded
|
|
15
|
+
service.
|
|
16
|
+
email: promotos@gmx.de
|
|
17
|
+
executables:
|
|
18
|
+
- telnetd
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- lib/telnetd/telnet_client.rb
|
|
23
|
+
- lib/telnetd/telnet_server.rb
|
|
24
|
+
- lib/telnetd.rb
|
|
25
|
+
- bin/telnetd
|
|
26
|
+
- make_rdoc.bat
|
|
27
|
+
- Rakefile
|
|
28
|
+
- README.md
|
|
29
|
+
- telnetd-0.0.1.gem
|
|
30
|
+
- telnetd.gemspec
|
|
31
|
+
- test/test_telnet_server.rb
|
|
32
|
+
homepage: http://rubygems.org/gems/telnetd
|
|
33
|
+
licenses: []
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>'
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: 1.3.1
|
|
50
|
+
requirements: []
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 1.8.16
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 3
|
|
55
|
+
summary: Simple telnet daemon
|
|
56
|
+
test_files: []
|