How to start/run a ruby program in a remote machine in a LAN using a
ruby program in local machine?
Net-ssh. Or capistrano!
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!
Brandon W. wrote in post #1076989:
Either using ssh, the netssh gem, or by using sockets to implement
communication.Brandon W.
Thank you everybody… for you amazingly helping words… I think I’ll go
for socket as I am not feeling comfortable with netssh!!! thanks again!
On Sat, Sep 22, 2012 at 2:53 PM, ajay paswan [email protected]
wrote:
Brandon W. wrote in post #1076989:
Either using ssh, the netssh gem, or by using sockets to implement
communication.Brandon W.
Thank you everybody… for you amazingly helping words… I think I’ll go
for socket as I am not feeling comfortable with netssh!!! thanks again!
There’s also DRb. Although then you have to
a) ensure a DRb server is running on the other machine
b) do authentication yourself.
It all depends…
Kind regards
robert
Either using ssh, the netssh gem, or by using sockets to implement
communication.
Brandon W.