Ruby/Network/ssh
Материал из Wiki.crossplatform.ru
(Различия между версиями)
Версия 17:10, 26 мая 2010
Содержание |
An SSH Client
require "rubygems" require "net/ssh" Net::SSH.start("example.ru", :username=>"userName", :password=>"mypass") do |session| end
Change directory with SSH
require "rubygems" require "net/ssh" Net::SSH.start("example.ru", :username=>"yourName", :password=>"mypass") do |session| shell = session.shell.sync puts "#{shell.pwd.stdout}" shell.cd "test_dir" puts "#{shell.pwd.stdout}" puts "Directory contents:" puts shell.ls("-l").stdout shell.exit end
Copying a File to Another Machine
require "rubygems" require "net/ssh" def copy_file(session, source_path, destination_path=nil) destination_path ||= source_path cmd = %{cat > "#{destination_path.gsub(""", "\"")}"} session.process.popen3(cmd) do |i, o, e| puts "Copying #{source_path} to #{destination_path}... " open(source_path) { |f| i.write(f.read) } puts "Done." end end Net::SSH.start("example.ru", :username=>"yourName", :password=>"mypass") do |session| copy_file(session, "/home/test.rb") end
List server files with SSH
require "rubygems" require "net/ssh" Net::SSH.start("example.ru", :username=>"userName", :password=>"mypass") do |session| cmd = "ls -l /home" session.process.popen3(cmd) do |stdin, stdout, stderr| puts stdout.read end end