ruby net-ssh调用带有交互式提示的bash脚本 [英] ruby net-ssh calling bash script with interactive prompts

查看:137
本文介绍了ruby net-ssh调用带有交互式提示的bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,希望您能为我提供帮助

I have a problem that I hope you can help me with

我正在尝试使用ruby ssh到计算机上并运行bash脚本,这部分相当简单,但是bash脚本要求我以交互方式输入用户名和密码,这就是我要坚持的地方

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

因此,如果我手动运行脚本,则会看到:-

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’] 

但是我不知道如何扩展Net :: SSH调用以响应提示

but I don’t know how to extend the Net::SSH call to respond to the prompts

  # 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

任何人有任何想法

推荐答案

设法通过使用channel函数来解决此问题,这是我现在使用的方法

managed to fix this by using the channel function, here's the method I use now

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

这篇关于ruby net-ssh调用带有交互式提示的bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆