1

Currently I am trying to write a ruby script to logging-in SSH Remote Server using "pty" & "expect" ruby library. and also try to create new rails app inside the remote system using that script.

Anyone can tell me. how logging-in to remote server using the ruby library ?

Siva KB
  • 357
  • 1
  • 6
  • 19

1 Answers1

1

You should look at some of the examples for Net::SSH, it basically opens a connection and gives you a block to execute whatever command you like, e.g.:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '192.168.1.113'
USER = 'username'
PASS = 'password'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
  output = ssh.exec!('ls')
  puts output
end

(taken from here)

I haven't done anything with it, but you get the idea. Given this construct you can run whatever program you like on the remote server and parse its output. As you are probably interested in public key authorization you should have a look at the answer to this question, it will show you how to specify a key-file.

Community
  • 1
  • 1
Patru
  • 4,481
  • 2
  • 32
  • 42
  • I used the net::ssh it's working fine, but my requirement is remote severer logging in using ruby library pty and expect. did you know about that ? – Siva KB Jun 26 '14 at 11:35
  • @PaulWalker: Neither `pty` nor `expect` is a sufficiently specific description of a piece of software, so I do not now what you mean and I have no means to look it up (provide links to github repos to amend this). Being ruby-gem I assume you want to write some script with them and you can put it on the server, then you can run it through `Net::SSH` as above. – Patru Jun 26 '14 at 14:47
  • I want the same functionality, their [Net::SSH::Channel#request_pty](http://goo.gl/sZbJbr) method, but I cannot figure how to use it. The problem with Channel#exec and Session#exec! methods is that are not stateful, and they open a new connection with each request. so if you do for example ssh.exec!('cd /home') while you connect as the root user, then ssh.exec!('pwd') you will get '/root'. – Nafaa Boutefer Jun 28 '14 at 09:32