Caveman
5/10/2007 7:46:00 PM
Saga,
There are many ways to execute commands via the NET::SSH. Here is one
option that will do what you want it to do:
This encapsulates the commands:
Net::SSH.start(h,u,p) do |session|
shell = session.shell.sync
cmd = shell.send_command "whatevercommand\n"
p cmd.stdout
cmd2 = shell.send_command "whatevercommand\n"
p cmd2.stdout
end
end
Or, if it's a standard command, you can do this:
Net::SSH.start(h,u,p) do |session|
shell = session.shell.sync
cmd = shell.whatevercommand
p cmd.stdout
cmd2 = shell.whatevercommand
p cmd2.stdout
end
end
Cool thing about this is, if you are running a command that pipes to
stderr, you can also capture that with cmd.stderr.
Using the shell.send_command method allows you to send entire strings
to be executed, such as:
cmd = shell.send_command("cat /proc/partitions")
p cmd.stdout
Which allows you to capture the stdout as a string, then parse it as
such:
cmd = shell.send_command("cat /proc/partitions")
devices = out.stdout
devices.gsub!(/\n/,'')
devices.gsub!(/([0123456789])/, '')
disks<<devices.scan(%r/ (.*) /)
puts disks.uniq!
Which "should" give you a list of all unique mounted drives in your
system... just as an example.
Hope this helps,
Caveman