如何 SSH 交互式会话 [英] How to SSH interactive Session

查看:83
本文介绍了如何 SSH 交互式会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要在 linux 机器上执行命令这个命令是交互式的命令.交互式命令意味着需要输入 [yes , no] 或密码两次

I need to execute command on linux machine this command is interactive command. Interactive command means require input like [yes , no] or password twice

我的真实情况是.我创建了一个脚本执行命令并成功获取输出.但有些服务器的登录密码已过期,所以我需要与服务器发送当前密码+新密码(两次)

my real case is. I create a script execute commands and get the outputs successfully. but some servers has Loging password expired so I need to interact with server to send the current password + the new password(twice)

ssh userName@10.0.0.243
userName@10.0.0.243's password:
You are required to change your password immediately (password aged)
Last login: Sun Aug  7 13:15:40 2011 from 10.0.0.28
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user userName.
Changing password for userName
(current) UNIX password:
New UNIX password:
Retype new UNIX password:

注意事项::

  • 我使用的是 Ruby 1.9.2
  • 我在正常情况下执行命令没有问题
  • 拜托,我必须避免使用 (echo "pass" | ssh -S) 之类的解决方法来让我通过任何其他交互情况.
  • 我正在使用net/ssh"库
  • 附上脚本http://king-sabri.net/files/LinuxHWScanner.rb
  • 我尝试了net/ssh/telnet",但没有帮助
  • 一些建议告诉使用rake/remote_task"是解决方案,但我无法理解它在我的情况下是如何工作的

如果你需要更简单,这里有一个简单的代码,如果你让它工作,我想它会解决我以前的问题

if you need more simplicity, here a simple code if you make it works I thing it'll solve my previous issue

  require 'net/ssh'

  host = "10.0.0.106"
  port = 22     # SSH port
  user = 'root'     # username
  pass = "123123"   # password

Net::SSH.start( host,user,:password => pass, :port=> port , :verbose => :error ) do |session|

 puts session.exec!("passwd root")
end

推荐答案

类似的事情?

Net::SSH.start('10.0.0.6', 'not_root', :password => "test") do |ssh|
   ssh.open_channel do |channel|
      channel.on_request "exit-status" do |channel, data|
        $exit_status = data.read_long
      end
      channel.exec("passwd") do |channel, success|
        if success
          channel.on_data do |channel, data|
            # Don't really need this callback actually
            puts "got data: #{data.inspect}"
          end
          # You don't need this line if you're root
          channel.send_data("oldpass\n")
          channel.send_data("newpass\n")
          channel.send_data("newpass\n")
        else
           puts "FAILED"
        end
      end
      channel.wait
      puts "SUCCESS" if $exit_status == 0
   end
end

这个很脏,但在过早的到期提示和 passwd 发出时都适用:

This one is dirty, but working for both upon premature on-expiration prompt and upon passwd issuing:

Net::SSH.start('localhost', 'not_root', :password => "test") do |ssh|
  ssh.open_channel do |channel|
     channel.on_request "exit-status" do |channel, data|
        $exit_status = data.read_long
     end
     channel.on_data do |channel, data|
        puts data.inspect
        if data.inspect.include? "current"
                channel.send_data("oldpass\n");
        elsif data.inspect.include? "new"
                channel.send_data("newpass\n");
        end
     end
     channel.request_pty
     # This will just safely fail if we have already a password prompt on
     channel.exec("passwd");
     channel.wait
     # Will reflect a valid status in both cases
     puts $exit_status
  end
end

这篇关于如何 SSH 交互式会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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