简体   繁体   English

ruby net-ssh调用带有交互式提示的bash脚本

[英]ruby net-ssh calling bash script with interactive prompts

I have a problem that I hope you can help me with 我有一个问题,希望您能帮助我

I'm trying to use ruby to ssh onto a machine and run a bash script, this part is fairly easy but the bash script requires me to entry a username and password interactively and this is where I'm stuck 我正在尝试使用ruby SSH到计算机上并运行bash脚本,这部分相当简单,但是bash脚本要求我以交互方式输入用户名和密码,这就是我遇到的问题

So if I run the script manually I see:- 因此,如果我手动运行脚本,我会看到:-

./run_file.sh
Enter username:
Enter password:

So at the enter Username prompt I have to enter the username etc 所以在输入用户名提示时,我必须输入用户名等

I've got a simple method I use to make the connection and my idea was to pass in an array made up of 我有一个简单的方法来建立连接,我的想法是传入一个由

[‘command to run’, ‘username’, ‘password’] 

but I don't know how to extend the Net::SSH call to respond to the prompts 但我不知道如何扩展Net :: SSH调用以响应提示

  # ssh conectivity method
  def command_ssh(host, cmd)
    require 'net/ssh'
   user = LocalConfig::SSH_DETAILS[:user]
   pass = LocalConfig::SSH_DETAILS[:pass]

    Net::SSH.start(host, user, :password => pass, :paranoid => false, :auth_methods => ['password'], :timeout => 10 )do |ssh |
      output = (ssh.exec!(cmd[0]))
      return output
    end
  end

anyone got any ideas 任何人有任何想法

managed to fix this by using the channel function, here's the method I use now 通过使用channel函数设法解决了这个问题,这是我现在使用的方法

def connect(host,command)

  require 'rubygems'
  require 'net/ssh'

  user = LocalConfig::SSH_DETAILS[:user]
  pass = LocalConfig::SSH_DETAILS[:pass

    o = ""
    Net::SSH.start(host, user, :password => pass, :paranoid => false, :auth_methods => ['password'], :timeout => 30 )do |ssh |
      channel = ssh.open_channel do |ch|
    ch.exec(command) do |ch2, success|
      ch2.send_data "myUserName\n"
      ch2.send_data "mPassWord\n"

      ch.on_data do |ch2, data|
        o += data
      end
    end
      end
      channel.wait
      return o.to_s
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM