?
Solved

How to SSH interactive Session In ruby Ruby

Posted on 2011-10-29
3
Medium Priority
?
1,421 Views
Last Modified: 2012-05-12
Good day everyone

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:
----------------------------

Notes::
- I'm using Ruby 1.9.2
- I've no problem in executing command in normal case
- please, I have to avoid workarounds like (echo "pass" | ssh -S) to
make me pass any other interactive situations.
- I'm using 'net/ssh' libs
- The Script is Attached
http://king-sabri.net/files/LinuxHWScanner.rb
- I tried "net/ssh/telnet" and it doesn't help
- Some advises tell useing 'rake/remote_task' is the solution but I can't understand how it works in my case

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

Open in new window





please I need it urgently



0
Comment
Question by:eng-sabri
  • 3
3 Comments
 
LVL 1

Accepted Solution

by:
eng-sabri earned 0 total points
ID: 37052190
Actually , community was faster than experts exchange :(

the solution is
#--> Case 1:Need bypass pass  password expire
require 'net/ssh'

Net::SSH.start('10.0.0.106', 'userName', :password => "currentPass") 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("currentPass\n");
        elsif data.inspect.include? "New"
                channel.send_data("NewPass\n");
	elsif data.inspect.include? "new"
                channel.send_data("NewPass\n");

	sleep 0.1
        end
     end
     channel.wait
     # Will reflect a valid status in both cases
     puts $exit_status
  end
end

Open in new window


#--> Case2: Need change pass remotely normal case
require 'net/ssh'

Net::SSH.start('10.0.0.106', 'user', :password => "123123") do |ssh|
   ssh.open_channel do |channel|
      channel.on_request "exit-status" do |channel, data|
        $exit_status = data.read_long
      end
     channel.request_pty do |channel, success| 
     channel.exec("sudo passwd UserName")  # Logged user shuold be root or sudoers memeber
        if success
          channel.on_data do |channel, data|
	    puts data.inspect.chomp("\r\n")
	    channel.send_data("123123\n")
	    sleep 0.1
          end
        else
           puts "FAILED!!"
        end
      end
      channel.wait
      puts "SUCCESS!!" if $exit_status == 0
   end
end

Open in new window

0
 
LVL 1

Author Comment

by:eng-sabri
ID: 37052410
0
 
LVL 1

Author Closing Comment

by:eng-sabri
ID: 37052425
I solve my issue ,, Thanks to Community
rely is late
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question