Automating Ssh to Windows with Ruby

Automating SSH to windows with Ruby

I tried it out in Pry and found two issues:

  1. HOST is undefined, it should be host as this is the variable being passed into the block.
  2. SSH.start expects the parameters to be STRING class, so add the .to_s as indicated below.

Also, I switched it to the idiomatic Ruby pattern of using do...end when a block extends past 1 line.

hosts.each do |host|
puts "SSHing #{host} ..."
Net::SSH.start( host.to_s, USER, :password => PASS ) do |ssh|
puts ssh.exec!('date')
puts "Logging out..."
end
end

I tested this in Pry and it's now working. I hope this helps.

How to test SSH connection using Ruby

By searching a bit in StackOverflow I've found this thread
and I was able to solve my problem doing this:

def ssh_try(user, host, pass)
puts "SSHing #{host} ..."
Net::SSH.start( host.to_s, user.to_s, :password => pass.to_s ) do |ssh|
puts ssh.exec!('date')
puts "Logging out..."
end
end

Anyone who is facing a similar problem can try this method, works great to test/use ssh connection in ruby.

Calling a remote command via ssh, using Ruby, improved syntax?

I would make it a little pretty, if that is what you are after:

#!/usr/bin/env ruby
# encoding: utf-8

home = "/path/to/users/home/on/remote"
binary = File.join(home, "bin", "command")
command = "#{binary} '#{parameter}'"
puts `ssh user@host #{command}`

Otherwise, you can use the net-ssh library, and do something like this:

#!/usr/bin/env ruby
# encoding: utf-8

require 'net/ssh'
Net::SSH.start('host', 'user', :password => "<your-password>") do
home = "/path/to/users/home/on/remote"
binary = File.join(home, "bin", "command")
command = "#{binary} '#{parameter}'"
output = ssh.exec!(command)
puts output
end

There are, obviously, automated ways of capturing remote user's home directory path, but I skipped that part.

ruby - copy a jar from linux to windows server

If you are already using SSH in your build script I would think the easiest thing to do would be to install an SSH server on the Windows box. You can use sshd via Cygwin or opt for a commercial product like WinSSHD (I'm sure there are numerous other free or paid products out there).

Once you have that running you can more than likely reuse most of the existing script relating to moving files around and use a gem like Win32Utils to do any windows specific tasks.

Writing Automated scripts to configure device

Step 1: decide the file structure of your program.

For example, this is the simplest structure

if_admin/
|--config.yml
|--run.rb

Step 2: write a config file or a bunch of config files that contain the different parts of the commands you need to run on the targets.

For example, you can use a yaml file like this:

xx.xx.xx.xx:
password: s3cret
router-shelf: x
slot: x
port: x
yy.yy.yy.yy:
...

Step 3: implement what you want to do

require 'yaml'
require 'net/telnet'

config = YAML.load_file('./config.yml')
config.each do |host, conf|
telnet = Net::Telnet.new('Host' => host)
telnet.login(conf['password'])
telnet.puts <<-CMD
sys
interface g #{conf['router-shelf']}/#{conf['slot']}/#{conf['port']}
shut
desc free-port
CMD
telnet.close
end

Stuck on a system call with variables

I'd guess that you want this:

system($x, 'a', 'library.7z', $library, '-r')

That would give you the same effect as this at the command prompt:

C:\Program Files\7-Zip\7z.exe a library.7z "C:\Users\maste_000\Documents\Calibre Library" -r

Assuming, of course, that I haven't completely forgotten how quoting works with the Windows command line.

The space in $library won't matter when you use the multi-argument form of the system; part of the reason that you use this version of system is to avoid all the weird quoting that you need when the shell is in the way. The main thing is that you probably need to separate 'a' and 'library.7z' so that they are two arguments rather a single argument with a space as the second character; without that separation you'd be saying this:

C:\Program Files\7-Zip\7z.exe "a library.7z" "C:\Users\maste_000\Documents\Calibre Library" -r

and that doesn't look right.



Related Topics



Leave a reply



Submit