How an interactive SSH session

Good afternoon everyone

I need to execute a command on a Linux machine, this command is an interactive command. An interactive command requires input, for example [yes, no] or a password twice

my real case is. I create script execution commands and successfully get the results. but on some servers the password expires, so I need to interact with the server to send the current password + new password (twice)

ssh userName@10.0.0.243 userName@10.0.0.243 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: 

Notes::

  • I am using Ruby 1.9.2
  • I have no problem executing the command in the normal case
  • please, I should avoid workarounds like (echo "pass" | ssh -S) to get me to go through any other interactive situations.
  • I use 'net / ssh' libs
  • script attached http://king-sabri.net/files/LinuxHWScanner.rb
  • I tried "net / ssh / telnet" and it does not help
  • Some tips say that using "rake / remote_task" is a solution, but I can't figure out how this works in my case.

if you need more simplicity, here is simple code, if you do this, I believe that it will solve my previous problem.

  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 
+4
source share
1 answer

Something like that?

 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 

This file is dirty, but it works both at a premature start and after passwd output:

 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 
+3
source

All Articles