summaryrefslogtreecommitdiff
path: root/test_server.rb
blob: af24980d03c362de195f6968a8f0471b51ab4f61 (plain)
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
$:.push('./lib')
require 'net/ssh'
require 'net/ssh/server'
require 'net/ssh/server/keys'
require 'net/ssh/transport/server_session'
require 'socket'
require 'ostruct'
require 'byebug'

PORT = 2000
Thread.abort_on_exception=true


logger = Logger.new(STDERR)
logger.level = Logger::DEBUG

puts "Setting up server keys..."
server_keys = Net::SSH::Server::Keys.new(logger: logger, server_keys_directory: '.')
server_keys.load_or_generate

puts "Precomputing dh keys..."
key_sizes = [1024]
server_dhs = Hash[key_sizes.map {|i| [i,OpenSSL::PKey::DH.new(i)]}]

puts "Listening on port #{PORT}..."
Thread.start do
  server = TCPServer.new PORT
  header = []
  loop do
    Thread.start(server.accept) do |client|
      options = {}
      options[:logger] = logger
      options[:server_side] = true
      options[:server_keys] = server_keys.keys
      options[:host_key] = server_keys.types
      options[:kex] = ['diffie-hellman-group-exchange-sha256']
      options[:hmac] = ['hmac-md5']
      options[:server_dh] = server_dhs
      session = Net::SSH::Transport::ServerSession.new(client,options)
      session.run_loop do |connection|
        connection.on_open_channel('session') do |session, channel, packet|
          channel.on_request 'shell' do |channel,data|
            command = data.read_string
            puts "received command:#{command}"
            channel.send_data "reply to :#{command}"
          end
          channel.on_request 'exec' do |channel,data|
            command = data.read_string
            puts "received command:#{command}"
            channel.send_data "reply to :#{command}"
           end
        end
      end
    end
  end
end

sleep(1)
#Net::SSH.start('localhost', 'boga', port: PORT, password: "boga", verbose: :debug) do |ssh|
#  output = ssh.exec("hostname") 
#end
sleep(160)
puts "END"