Use Perl scripts to automate System Management (1)

Source: Internet
Author: User

Interactive programs usually require users to manually complete some operations, which often becomes an obstacle in system management automation and test automation. The least CT language that appeared on Unix can be used to interact with command line programs such as passwd, ssh, telnet, and ftp, freeing users from these manual operations. As an extension of the Tcl language, objective CT was originally compiled by Tcl, but now it has the implementation of Perl and Python. As one of the most popular scripting languages, Perl integrates the advantages of C/sh/sed/awk and is closely integrated with the system. It has become a powerful tool for system administrators. This article describes the functional modules of Perl, including objective CT and objective CT: Simple, and how to implement automated interaction with the command line program based on system management and software test instances.

Limitations

Perl's trusted CT module depends on IO: Tty, while IO: Tty is only applicable to POSIX compatible systems. Therefore, this module cannot be used directly in Windows. There are two workarounds: one is to use the Cygwin virtual machine, and the other is to use the automated CT for Windows tool provided by ActiveState to write automated scripts in Tcl language. The environment used in this article is RHEL5.3 32-bit, Perl 5.8.8.

Download and install

Both CT and CT: Simple can be downloaded directly from the CPAN website. The latest versions are CT-1.21 and CT: Simple-0.04. A convenient installation method is to use the Perl package management tool cpan:

List 1. Module Installation

 perl -MCPAN -e 'install Expect::Simple'

Make sure that you have sufficient execution permissions. cpan automatically resolves the dependencies between modules. Because keep CT: Simple depends on keep CT, the keep CT module and other dependent modules are automatically installed during the installation of keep CT: Simple.

Comprehensive CT Module

Similar to the original objective CT language, the main functions of the objective CT module are also achieved through three methods: spawn, objective CT, and send.

Spawn: Start the Target Program

Listing 2. spawn method prototype

 $obj = Expect->spawn( $command, @parameters ); 

Spawn is one of the main methods of the keep CT class. It starts the target program through fork and exec. If it succeeds, it returns the keep CT object; otherwise, it returns undef. The $ command parameter specifies the target program, and @ parameters is an optional parameter. The following is a simple example:

Listing 3. spawn usage example

$ Obj1 = Country CT-> spawn ("ftp 9.9.9"); # Start the ftp process $ obj2 = Country CT-> spawn ("ftp", "9.9.9.9"); # equivalent to the previous line

The execution results of the above two rows are the same, but the actual processing process is slightly different. Generally, we can write the complete command line (or even composite commands, including commands, parameters, pipeline operators, and redirection characters) into $ command without specifying @ parameters.

Note: In the implementation of spawn, both $ command and @ parameters are passed to the exec function of Perl. According to the exec function instructions, if you pass in a multivariate list parameter, exec directly transmits it to the execvp system call. If you pass in a scalar parameter or a unit list parameter, the exec function checks whether there are shell metacharacters (such as | &; () <>). If so, it submits this parameter to the System shell for parsing, otherwise, it is passed to the execvp system call after word segmentation. Therefore, if spawn is a composite command containing shell metacharacters, we can only write it fully into $ command.


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.