Turn from Ruby fans: write Linux server Management scripts using NET::SSH and NET::SCP

Source: Internet
Author: User

Try Perl under the installation of SSH module, the whole half a day linux/window on all, the version of the dependent module is always matching, after changing the idea, with Ruby Bar

Net::ssh and NET::SCP are two gem packages that operate SSH with Ruby. Net::ssh is equivalent to CMD, specifically for executing commands; NET::SCP is dedicated to transferring files. Together, they can do anything that the SSH client can do.

Installation:
Gem Install Net-sshgem Install NET-SCP

All of the following code references this code

Require ' net/ssh ' require ' net/scp ' HOST = ' 192.168.1.1 ' USER = ' username ' PASS = ' password '
1. Use Net::ssh to execute a command
Net::ssh.start (HOST, USER,:p assword = PASS) do |ssh|   result = ssh.exec! (' ls ')   Puts Resultend

Net::ssh.start will establish a connection with the target host and return a session that represents the connection. If a block is received later, the connection is automatically closed at the end of the block. Otherwise, you should close the connection yourself. Note that the password is passed as a hash parameter, because the SSH login authentication method is more, the parameters need varied.

2. Use Net-sftp to transfer files.

If you do not need to execute a command, just transfer files, you can use Net::scp.start, similar to Net::ssh.start

Net::scp.start (HOST, USER,:p assword = PASS) do |scp|   scp.upload! (' c:/scp1.rb ', '/home/oldsong/')   scp.download! ('/home/oldsong/test.txt ', ' c:/') End
3, if you want to transfer files, but also to execute the command, SCP does not have to re-establish the connection, borrowing SSH connection can
Net::ssh.start (HOST, USER,:p assword = PASS) do|ssh|  LogFiles = ssh.exec! (' ls *.log '). Split  Logfiles.each do |l|    ssh.scp.download! (l, L)  EndEnd
4, if you want to transfer large files, it is best to show the progress of transmission, otherwise long time no response, but also thought that the crash.
Net::ssh.start (HOST, USER,:p assword = PASS) do|ssh|  ssh.scp.upload! (' Large.zip ', '. ') Do|ch, name, sent, total|    Print "\r#{name}: #{(Sent.to_f * 100/total.to_f). to_i}%"  endend
5. Upload a directory, including all the files in the subdirectory. Add ": Recursive = true" parameter.
Net::ssh.start (HOST, USER,:p assword = PASS) do|ssh|  ssh.scp.download! (' Logs ', '. ',: recursive = true) end
6, if you do not want to save the file after downloading, but put in memory directly processing, as long as not to download! pass local file name, will return a string.
Net::scp.start (HOST, USER,:p assword = PASS) do|scp|  Puts scp.download! (' Log.txt '). Split (/\n/). grep (/^error/) End
7, SCP highest-level application, according to the event display all transmission information.
Net::scp.start (HOST, USER,:p assword = PASS) do|scp|  sftp.upload! (f, Remote_file) do |event, uploader, *args|case event  # Args[0]: File metadata  when:openputs "Start uploading.# {args[0].local} #{args[0].remote} #{args[0].size} bytes} "When  :p ut then# args[0]: File metadata# args[1]: byt e offset in remote file# args[2]: Data being written (as String) puts "writing #{args[2].length} bytes to #{args[0].remote } starting at #{args[1]} "  when:close then# args[0]: File Metadataputs ' finished with #{args[0].remote} '  WHEN:MK Dir then# args[0]: Remote path nameputs "Creating directory #{args[0]}"  when:finish thenputs "All done!" End  End  puts "upload success" end
Tags: Linux, net-scp, Net-ssh, NET::SCP, Net::ssh, Ruby
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.