簡體   English   中英

Errno::ENOTTY 在 SuSe 上通過 Net::SSH 連接到遠程服務器時,設備的 ioctl 不合適(使用 Ruby on Rails 5.2.4)

[英]Errno::ENOTTY Inappropriate ioctl for device when connecting to a remote server through Net::SSH on SuSe (with Ruby on Rails 5.2.4)

我的 Ruby on Rails 應用程序遠程啟動遠程 SuSe 服務器 (SUSE Linux Enterprise Server 15 SP2) 上的一些腳本。 它依賴於 Gemfile 中聲明的 net-ssh gem: gem 'net-ssh'

該腳本通過以下塊遠程觸發:

    Net::SSH.start(remote_host, remote_user, password: remote_password) do |ssh|
      feed_back = ssh.exec!("#{event.statement}")
    end

只要 Rails 服務器在 Windows Server 2016(我的 DEV 環境)上運行,它就會按預期工作。 但是當我部署到驗證環境(即 SUSE Linux Enterprise Server 15 SP2)時,我收到以下錯誤消息:

Errno::ENOTTY in myController#myMethod 
Inappropriate ioctl for device

另一方面,通過命令行發出 SSH 請求 - 從 SUSE 到 SUSE - 也按預期工作。 四處閱讀我沒有找到 Net::SSH 模塊的相關參數來解決這個問題。

歡迎您的建議!

我終於發現該消息指的是 SSH 的操作模式:它需要一種終端仿真 - 所謂的pty - 封裝到 SSH 通道中。

所以我是這樣實現的:

Net::SSH.start(remote_host, remote_user, password: remote_password) do |session|
  session.open_channel do |channel|
    channel.request_pty do |ch, success|
      raise "Error requesting pty" unless success
      puts "------------ pty successfully obtained"
    end

    channel.exec "#{@task.statement}" do |ch, success|
      abort "could not execute command" unless success

      channel.on_data do |ch, data|
        puts "------------ got stdout: #{data}"
        @task.update_attribute(:return_value, data)
      end

      channel.on_extended_data do |ch, type, data|
        puts "------------ got stderr: #{data}"
      end

      channel.on_close do |ch|
        puts "------------ channel is closing!"
      end

    end
  end

  ### Wait until the session closes
  session.loop
end

這解決了我的問題。

注意:上面提出的答案只是解決方案的一部分。 部署到生產服務器時,此源代碼再次出現相同的錯誤。

問題似乎是 SSH 目標的密碼:我手動重新輸入了它而不是從 MS Excel 執行通常的復制/粘貼,現在 SSH 連接成功了!

由於引發的錯誤不是簡單的“連接被拒絕”,我懷疑密碼字符串具有特定的字符編碼或意外的結束字符。

由於第一個提議的解決方案提供了一個工作示例,我將其留在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM