#!/usr/bin/env ruby
# frozen_string_literal: true

require 'net/ssh'
require 'net/ssh/telnet'

# 测试 Net::SSH 直接连接
puts "=== 测试 Net::SSH.start 直接连接 ==="
begin
  ssh = Net::SSH.start('127.0.0.1', 'zb', port: 22, password: '', timeout: 10)
  puts "✓ Net::SSH.start 连接成功！"
  ssh.close
rescue => e
  puts "✗ Net::SSH.start 连接失败: #{e.message}"
  puts e.backtrace[0..5]
end

puts "\n=== 测试 Net::SSH::Telnet.new 不同参数方式 ==="

# 测试1: 基本方式（不带端口）
puts "\n1. 测试基本方式（不带端口参数）:"
begin
  conn = Net::SSH::Telnet.new(
    "Host" => "127.0.0.1",
    "Username" => "zb",
    "Password" => "",
    "Prompt" => /[$%#>]\s*$/
  )
  puts "✓ 连接成功!"
  conn.close
rescue => e
  puts "✗ 连接失败: #{e.message}"
end

# 测试2: 带 Port 参数
puts "\n2. 测试带 Port 参数:"
begin
  conn = Net::SSH::Telnet.new(
    "Host" => "127.0.0.1",
    "Port" => 22,
    "Username" => "zb",
    "Password" => "",
    "Prompt" => /[$%#>]\s*$/
  )
  puts "✓ 连接成功!"
  conn.close
rescue => e
  puts "✗ 连接失败: #{e.message}"
end

# 测试3: 带 ssh_opts 参数
puts "\n3. 测试带 ssh_opts 参数:"
begin
  conn = Net::SSH::Telnet.new(
    "Host" => "127.0.0.1",
    "Username" => "zb",
    "Password" => "",
    "Prompt" => /[$%#>]\s*$/,
    "ssh_opts" => { port: 22 }
  )
  puts "✓ 连接成功!"
  conn.close
rescue => e
  puts "✗ 连接失败: #{e.message}"
end

# 测试4: 查看 Net::SSH::Telnet 的源码
puts "\n=== 查看 Net::SSH::Telnet 如何处理参数 ==="
begin
  gem_path = Gem.find_files('net/ssh/telnet.rb').first
  if gem_path
    puts "net-ssh-telnet 位置: #{gem_path}"
    content = File.read(gem_path)
    puts "\ninitialize 方法片段:"
    content.each_line do |line|
      if line.include?('def initialize') || line.include?('Net::SSH.start')
        puts line.strip
      end
    end
  end
rescue => e
  puts "无法读取源码: #{e.message}"
end
