From: Cliff Rosson Date: 2012-09-22T01:51:22+09:00 Subject: Re: ruby - remote method invocation --20cf30334d3195a34f04ca390c3d Content-Type: text/plain; charset=ISO-8859-1 I am no expert but I use this to manage devices. require 'rubygems' require 'net/ssh' require 'net/ssh/telnet' class SSH attr_accessor :errors def initialize(creds) begin @ssh_session = Net::SSH.start(creds[:host], creds[:user], :password => creds[:password], :keys => []) @ssh = Net::SSH::Telnet.new("Session" => @ssh_session, "Prompt" => creds[:prompt]) @errors = false rescue Exception => e @errors = e end end def cmd(command) @ssh.cmd(command) end def close @ssh_session.close end end I would then create something like this depending on the device type I was connecting to. require './ssh.rb' class Cisco_ssh < SSH attr_accessor :config, :users def initialize(creds) begin @host = creds[:host] @users = [] creds[:prompt] = /.*>|.*#/ super(creds) @ssh.cmd("en\r#{creds[:enable]}") @ssh.cmd("term len 0") @config = @ssh.cmd("show run") get_users rescue Exception => e @errors = e end end def enable(pass) @ssh.cmd("en\r#{pass}") end def termlen=(length) @ssh.cmd("term len #{length}") end def close termlen=24 @ssh_session.close end def update_config @config = @ssh.cmd("show run") get_users end private def get_users @config.lines.each do |line| if line.include? "username" user = {} user[:username] = line.split[1] user[:password] = line.match(/(password|secret).\d.*/)[0].split[2] @users.push(user) end end end end This stuff is pretty old so I should probably review it and clean it up. Anyhow feel free to use it. I am still learning Ruby myself. Criticisms are welcome! On Fri, Sep 21, 2012 at 12:38 AM, Jam Bees wrote: > Net-ssh. Or capistrano! > > On Sep 21, 2012, at 12:29 AM, ajay paswan wrote: > > > How to start/run a ruby program in a remote machine in a LAN using a > > ruby program in local machine? > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > -- http://about.me/cliffrosson vizualize.me/cliffrosson --20cf30334d3195a34f04ca390c3d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I am no expert but I use this to manage devices.

re= quire 'rubygems'
require 'net/ssh'
requ= ire 'net/ssh/telnet'

class SSH
=A0 attr_accessor :errors
=A0=A0
=A0 def initialize(cre= ds)
=A0 =A0 begin
=A0 =A0 =A0 @ssh_session =3D Net::SSH= .start(creds[:host], creds[:user], :password =3D> creds[:password], :key= s =3D> [])
=A0 =A0 =A0 @ssh =3D Net::SSH::Telnet.new("Session" =3D> @ssh_= session, "Prompt" =3D> creds[:prompt])
=A0 =A0 =A0 @= errors =3D false
=A0 =A0 rescue Exception =3D> e
=A0= =A0 =A0 @errors =3D e
=A0 =A0 end
=A0 end
=A0=A0
=A0 def cmd(comman= d)
=A0 =A0 @ssh.cmd(command)
=A0 end
=A0=A0
=A0 def close
=A0 =A0 @ssh_session.close
=A0 e= nd
=A0 =A0=A0
end

I would then create something like this depend= ing on the device type I was connecting to.

<= div>require './ssh.rb'

class Cisco_ssh <= ; SSH
=A0 attr_accessor :config, :users
=A0 def initialize(creds)<= /div>
=A0 =A0 begin
=A0 =A0 =A0 @host =3D creds[:host]
<= div>=A0 =A0 =A0 @users =3D []
=A0 =A0 =A0=A0
=A0 =A0 = =A0 creds[:prompt] =3D /.*>|.*#/
=A0 =A0 =A0 super(creds)
=A0 =A0 =A0 @ssh.cmd("en\r#{cr= eds[:enable]}")
=A0 =A0 =A0 @ssh.cmd("term len 0")=
=A0 =A0 =A0=A0
=A0 =A0 =A0 @config =3D @ssh.cmd("= show run")
=A0 =A0 =A0 get_users
=A0 =A0 rescue Exception =3D> e
=A0 =A0 =A0 @errors =3D e=
=A0 =A0 end
=A0 end
=A0=A0
=A0 def= enable(pass)
=A0 =A0 @ssh.cmd("en\r#{pass}")
=A0 end
=A0=A0
=A0 def termlen=3D(length)
=A0 =A0 @ssh.cmd("term len #{leng= th}")
=A0 end
=A0=A0
=A0 def close
=
=A0 =A0 termlen=3D24
=A0 =A0 @ssh_session.close
= =A0 end
=A0=A0
=A0 def update_config
=A0 =A0 @config =3D @ssh.cmd("sho= w run")
=A0 =A0 get_users
=A0 end
=A0=A0=
=A0 private
=A0 def get_users
=A0 =A0 @confi= g.lines.each do |line|
=A0 =A0 =A0 if line.include? "username"
=A0 =A0 = =A0 =A0 user =3D {}
=A0 =A0 =A0 =A0 user[:username] =3D line.spli= t[1]
=A0 =A0 =A0 =A0 user[:password] =3D line.match(/(password|se= cret).\d.*/)[0].split[2]
=A0 =A0 =A0 =A0 @users.push(user)
=A0 =A0 =A0 end
=A0 = =A0 end
=A0 end
end

This stuff is pretty old so I should probably review it and clean it up. = Anyhow feel free to use it. I am still learning Ruby myself. Criticisms are= welcome!
On Fri, Sep 21, 2012 at 12:38 AM, Jam Bees <= span dir=3D"ltr"><jam@jamandbees.net> wrote:

On Sep 21, 2012, at 12:29 AM, ajay paswan <lists@ruby-forum.com> wrote:

> How to start/run a ruby program in a remote machine in a LAN using a > ruby program in local machine?
>
> --
> Posted via ht= tp://www.ruby-forum.com/.
>




--
=
http://about= .me/cliffrosson
vizualize.me/cliffrosson
--20cf30334d3195a34f04ca390c3d--