Use the power of Ruby for more effective system management

Source: Internet
Author: User
Article Title: more effective system management using the power of Ruby. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

In addition to being used as a powerful Web application development platform, Ruby is rarely mentioned in combination with the Rails framework, which is a powerful scripting language, just like Python or Perl. It has very powerful functions. Because it can use a lot of built-in and external libraries, it can be used to solve many script writing requirements in the system management work environment. Furthermore, Ruby programming is also very interesting!

Introduction

Ruby is an extremely rich, free, simple, scalable, portable, and object-oriented scripting language. Recently, it is widely used in the Web field. To a certain extent, this is attributed to the very powerful Web application development framework Rails, Which is exactly written in Ruby. Rails, also known as Ruby on Rails (ROR), provides a very powerful platform for rapid and effective Web application development. It is highly scalable. Many websites on the Web are built using Ruby on Rails.

In addition to using Rails as a Web application development platform, Ruby is rarely mentioned, that is, as a powerful scripting language, like Python or Perl. It has very powerful functions, because it can use a lot of built-in and external libraries, so it can use its power to solve many script programming requirements in the general system management work environment.

System Management requires a large number of scripts to make things simpler and more effective. Writing scripts can better meet user management, process management, file management, software package management, and other basic automation requirements than monotonous manual work. Ruby is very useful in this scenario. It has a set of good libraries to meet this requirement.


For this article, I assume that the reader has Ruby application knowledge. The basic examples provided here use pure Ruby, so any UNIX-like classes supported by Ruby can be used? System and Windows? . For more advanced Cfruby examples, a UNIX system is required. All the examples below are already in a Linux? Ruby v1.8.4 is used on the machine for testing. They should also be used in the latest Ruby version.

Ruby in practice

In the first example, search for files that match the specified mode in the specified path and provide detailed information about these files in a user-friendly manner. To achieve this goal, you don't have to rely on any command line utility, but you only need to use Ruby's built-in APIs. Therefore, this example can be run on any platform running Ruby.

In addition, this example shows how powerful Ruby is to simplify script writing. Rather than simply simulating the * nix "find" command, it is built on the command, so it has a strong customization capability when using Ruby.


Listing 1. Search for files that match the specified mode in a given path and display their details

Require 'Find'
Puts ""
Puts "----------------------- File Search -----------------------------------"
Puts ""
Print "Enter the search path :"
Searchpath = gets
Searchpath = searchpath. chomp
Puts ""
Print "Enter the search pattern :"
Pattern = gets
Pattern = pattern. chomp
Puts "----------------------------------------------------------------------"
Puts "Searching in" + searchpath + "for files matching pattern" + pattern
Puts "----------------------------------------------------------------------"
Puts ""
Find. find (searchpath) do | path |
If FileTest. directory? (Path)
If File. basename (path) [0] = ?.
Find. prune # Don't look any further into this directory.
Else
Next
End
Else
If File. fnmatch (pattern, File. basename (path ))
Puts "Filename:" + File. basename (path)
S = sprintf ("% o", File. stat (path). mode)
Print "Permissions :"
Puts s
Print "Owning uid :"
Puts File. stat (path). uid
Print "Owning gid :"
Puts File. stat (path). uid
Print "Size (bytes ):"
Puts File. stat (path). size
Puts "---------------------------------------------------"
End
End
End

In this example:

Lines 5th-11-request the user to provide the search path and search mode.
Row 3-use the "Find" method in the "find" class in Ruby to traverse the specified search path.
Row 3-check whether the detected file is a directory. If the directory is not ".", recursively traverse the directory.
Row 3-use the "fnmatch" method in the "File" class to check whether the detected File conforms to the specified mode.
Lines 25th-34-if the file meets the pattern, the detailed information of the file is printed.

The following is an example output of this script.


Listing 2. example output of the first example

[Root @ logan] # ruby findexample. rb

----------------------- File Search -----------------------------------

Enter the search path:/test

Enter the search pattern: *. rb
----------------------------------------------------------------------
Searching in/test for files matching pattern *. rb
----------------------------------------------------------------------

Filename: s. rb
Permissions 100644
Owning uid: 1
Owning gid: 1
Size (bytes): 57
---------------------------------------------------
Filename: test. rb
Permissions 100644
Owning uid: 0
Owning gid: 0
Size (bytes): 996
---------------------------------------------------
Filename: s1.rb
Permissions 100644
Owning uid: 1
Owning gid: 1
Size (bytes): 39
---------------------------------------------------

During system management, the most common requirement is to effectively use zip files to manage backups, or transfer a group of files from one computer to another. Ruby has many advantages in this regard. The second example is built on the basis of the first example, but contains a scenario where you need to package the searched files into a zip file.

The built-in zlib module can be used to process gzip files, which is good enough in most cases. However, here I will use another good Ruby library, "rubyzip", to create and process zip archives. Please refer to the reference section and find the download link. Note that this example uses pure Ruby, which does not depend on any command line utility currently provided on the computer.

[1] [2] [3] [4] [5] Next page

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.