Linux System Bulk Configuration script

Source: Internet
Author: User

System bulk Configuration Script user manual usage requirements
    • Linux operating system (RHEL,kylin, meditation rock available)

    • Local computer expect commands are available

    • ssh, scp commands available

Script Composition and function description
    • Boot Script- config.sh

      The script directs the execution of the entire script combination, prompting the user to enter the remote computer's IP address (range) and root password.

    • System configuration Script- sysinfo-collect.sh

      The script can customize the commands that need to be executed on the remote computer, with the bootstrap script and the response script, the tasks that can be accomplished are:

    1. Batch execution of commands on remote computers;

    2. Modify the remote computer configuration file;

    3. Batch distribution of files to remote computers;

    4. Collect files in batches from remote computers;

Response Script-Sysinfo-collect.exp The script is primarily used to respond to password input in the following steps:

    1. Delete files under the /var/tmp/ directory on the remote computer with the same folder name as the script;

    2. Copy all files under the file containing the local script to the /var/tmp directory of the remote computer;

    3. Execute the remote computer's system configuration script sysinfo-collect.sh;

    4. Copy the files under the /usr/appsoft/software/sysinfo_zj from the remote computer back to the local management side folder with the same name;

How to use
    1. Batch file Distribution:

      First, make sure that the remote computer command execution part of the sysinfo-collect.sh script is empty, that the previously written commands are executed at this runtime, and that the files and three scripts that need to be distributed are stored in the same folder, such as Configall Folder, execute the config.sh script, enter the remote computer IP address (range) and root password as required, and all files and scripts are copied to the specified remote computer.

    2. Batch System Configuration

      In the remote computer command execution section of the sysinfo-collect.sh script, edit the command to be executed remotely (if you need to use a variable, first customize the variable or declare the system variable, for example source /etc/bashrc ), and then perform the execution config.sh Script, enter the remote computer IP address (range) and root password as required.

    3. Batch File Collection

      In the remote computer command execution section of the sysinfo-collect.sh script, edit the command (if you want to use a variable, first customize the variable or declare the system variable, for example) to transfer the files that need to be source /etc/bashrc copied back to the local computer first to the---> ${sysinfo_dir} <---folder, then execute config.sh script, enter remote computer IP address (range) and root password as required , files that need to be searched are copied to the local folder---> /usr/appsoft/software/sysinfo_zj <---, and the files for each remote host are stored under "remote computer hostname +ip" Folder.

Using the example

Use script to simulate bulk replacement of Oracle Tnsnames.ora files, bulk modify the file belongs to the main group, bulk collection of remote computer hosts file.

A. Simulated environment preparation
IP Host name Password Notes
192.168.101.202 Zj-5-2 [Email protected] Local computer, expect command available, rhel_5.9
192.168.101.206 Zj-6-2 [Email protected] remote computer, rhel_6.5
192.168.101.207 Kylin3 [Email protected] remote computer, kylin_3.0

If each computer has an Oracle database installed, its environment variables are ORACLE_HOME set in file /home/oracle/.bash_profile :

[[email protected] ~]# grep ' oracle_home '/home/oracle/.bash_profile export Oracle_home=/home/oracle/software/ceshi

This test tnsnames.ora file use 123.sh instead, modify all the computer 123.sh file belongs to the main group oracle:oinstall , all computer /etc/hosts files are searched back to the local computer.

Now the contents of the 123.sh files in all computers are:

[Email protected] ~]# cat/home/oracle/software/ceshi/123.sh ABCDF
B. Script configuration and execution process

Local host 192.168.101.202 as the management side, only need the host expect command is available;

First upload three scripts to any folder on the management side, and then create the file 123.shthat needs to be distributed under that folder:

[Email protected] configall]# ls123.sh config.sh sysinfo-collect.exp sysinfo-collect.sh[[email protected] configall]# Cat 123.sh is a test. [Email protected] configall]#

Then edit the System configuration Script- sysinfo-collect.sh, declare the remote computer environment variables in the script, fill in the commands that need to be executed, and the example below:

################   to execute the command, please add it here--start   ###############################====================== =========================================================#★tips: #1 .  please use root user; #2 .  Files that need to be distributed to a remote computer are stored in the same directory as the script, and the files are copied to the--> ${dir}  folder on the remote computer, and the------#3 the .  script file on the remote computer, use the >   ${DIR}  <-------Replace #4 .  files that need to be copied back from the remote computer to the local--->  ${sysinfo_get}   or  ${SYSINFO_DIR}  <---#5.  ${sysinfo_dir}: Corresponds to local   "/usr/appsoft/software/ Sysinfo_zj "Directory;#    if stored in ${sysinfo_dir} folder, the proposed file name plus host name or IP, etc., to avoid different host files with the same name overwrite each other; #6 .  if the remote search file is stored to ${ Sysinfo_get} folder, the search back to local will be stored in the "/usr/appsoft/software/sysinfo_zj/hostname +ip" in different folders; #7 . ${dir}:  corresponding to the remote computer   "folder with the same name as the directory where the/var/tmp/script is located"; #需要在远程计算机执行的命令, add the following here: #======================================================= ========================= #声明远程计算机环境变量source  /home/oracle/.bash_profile# Copy file 123.sh to each computer $oracle_home MV  -f ${DIR}/123.sh  $ORACLE _home# Modify the file belongs to the main group chown -r oracle:oinstall ${oracle_home}/123.sh# the hosts file of each computer to retrieve the local cp -ar / etc/hosts ${sysinfo_get}################   to execute the command, please add it here--end   ######################## #########

After completing the above work, execute the config.sh script to configure the work:

Files copied back to the remote computer are stored in the /usr/appsoft/software/sysinfo_zj folder, and theconfig.sh script checks to see if the file exists on the local computer and does not exist to create There is a prompt whether to empty the existing files under the folder.

[[Email protected] configall]# sh config.sh Empty dir:/usr/appsoft/software/sysinfo_zj or not? (Yes|no): _

Follow the prompts to enter the start IP and terminate IP, here requires the local computer can ping all remote computers, and all the computer root password is the same, after the input is completed, press ENTER to continue;

        ***  File distribution and  configuration script for linux  ***         &NBSP;TIPS:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;1)  The   "expect"  command is available           &NBSP;&NBSP;&NBSP;&NBSP;2)  All the computer has the same password  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;3)  All files in  the same folder with scripts: config.sh       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;4)  Write your commands into the  SCRIPTS:&NBSP;SYSINFO-COLLECT.SHPLS&NBSP;INPUT&NBSP;THE&NBSP;FIRST&NBSP;IP:---192.168.101.202pls input &NBSP;THE&NBSP;LAST&NBSP;&NBSP;IP:---192.168.101.207pls input the password:[email protected] 

At this point, the script automatically distributes all the files under its folder to the remote computer /var/tmp/脚本同名文件夹 and executes the commands configured in the sysinfo-collect.sh script on the remote computer.

Examples of execution procedures:

Spawn ssh [email protected] cd /var/tmp/configall && rm -rf  *[email protected] ' s password: spawn scp -r -p /usr/appsoft/software/ Test/configall [email protected]:/var/tmp[email protected] ' s password: config.sh                                                                                                                                   100% 8833      8.6kb/s   00:00    sysinfo-collect.sh                                                                                                                          100% 4196     4.1kb/s   00:00    123.sh                                                                                                                                      100%     4     0.0KB/s   00:00     sysinfo-collect.exp                                                                                                                        100%  842     0.8KB/s   00:00     spawn ssh [email protected] cd /var/tmp/configall/ && sh  sysinfo-collect.sh[email protected] ' s password: ***   collecting  information of kylin3   ---[ running ]   ******    system information Collection Success    ---[ finished ]   ***spawn  Scp -r -p [email protected]:/usr/appsoft/software/sysinfo_zj/* /usr/appsoft/software/ Sysinfo_zj[email protected] ' s password: hosts                                                                                                                                       100%  185     0.2KB/s    00:00    * 192.168.101.207 *System information back  To the local computer---[ ok ]*******************************************************  192.168.101.202 *system information back to the  local computer---[ ok ]* 192.168.101.206 *system information back  to the local computer---[ ok ]* 192.168.101.207 *system  Information back to the local computer---[ ok ]* 192.168.101.203  *system information back to the local computer---[ FAILED ]*  192.168.101.204 *system information back to the local computer---[  failed ]* 192.168.101.205 *system information back to the local computer---[ FAILED ] 

During execution, the script skips a device that is not ping or ssh fails to log on, and gives a partial file distribution and script execution results.

The success of the script execution here is based on whether the host name +ip folder of the remote computer was recovered from the local computer's /usr/appsoft/software/sysinfo_zj directory, Does not indicate that all configuration commands are successful on the remote computer and are for reference only.


C. Script execution results

The following checks the command execution in the configuration script, which is expected to perform the following configuration commands:

#声明远程计算机环境变量source/home/oracle/.bash_profile# Copy file 123.sh to each computer $oracle_home mv-f ${dir}/123.sh $ORACLE _home# Modify the file belongs to the main group Chown-r Oracle:oinstall ${oracle_home}/123.sh# The Hosts file of each computer to retrieve local cp-ar/etc/hosts ${sysinfo_get}
    • Check that the 123.sh files on each computer have been replaced successfully and modified the owner and owner groups.

      Local computer:

[[email protected] ceshi]# Pwd/home/oracle/software/ceshi[[email protected] ceshi]# ll total 4-rw-r--r--1 Oracle Oinstall 18 07-14 09:42 123.sh[[email protected] ceshi]# cat 123.sh This is a test. [Email protected] ceshi]#

Remote computer:

[[email protected] configall]# Cd/home/oracle/software/ceshi/[[email protected] ceshi]# ll total dosage 4-rw-r--r--. 1 Oracle Oinstall 18 July 09:41 123.sh[[email protected] ceshi]# cat 123.sh This is a test. [Email protected] ceshi]#
    • Check that the Hosts file for each computer is searched back to the local computer.

 [[email protected] configall]# cd /usr/appsoft/software/sysinfo_zj/[[email  protected] sysinfo_zj]# lskylin3+192.168.101.207  log  zj-5-2+192.168.101.202   zj-6-2+192.168.101.206[[email protected] sysinfo_zj]# cat kylin3+ 192.168.101.207/hosts # do not remove the following line, or  various programs# that require network functionality will  fail.127.0.0.1localhost.localdomainlocalhost::1localhost6.localdomain6localhost6192.168.101.207 kylin3[[ email protected] sysinfo_zj]# cat zj-6-2+192.168.101.206/hosts 127.0.0.1    localhost localhost.localdomain localhost4 localhost4.localdomain4::1          localhost localhost.localudomain localhost6  Localhost6.localdomain6192.168.101.206 zj-6-2 
Script Generation folder description A. Script File storage folder

/var/tmp/script folder with the same name : The script will copy its folder to the /var/tmp directory of all computers by default, corresponding to ${dir in sysinfo-collect.sh script. Variable, it is recommended that you move files from this folder to the destination location, rather than copying, to avoid consuming /var/tmp directory space when distributing files to remote computers.

You can also configure the command in the sysinfo-collect.sh script: rm -rf ${DIR} Delete the folder after the other configuration is complete.

When using scripts to remove ${dir} , be careful not to delete the local computer, which can cause subsequent remote file distribution failures.

B. Search back the file storage folder

/usr/appsoft/software/sysinfo_zj: Corresponds to the ${sysinfo_dir} variable in the sysinfo-collect.sh script;

/usr/appsoft/software/sysinfo_zj/hostname +ip: ${sysinfo_get} in the corresponding sysinfo-collect.sh script variables;

If you need to bulk search the remote computer files back to the local, please dump the files into one of the two folders above, the two folders corresponding to the different search methods:

    • Dump the file into ${sysinfo_get} , the file will be stored in the /usr/appsoft/software/sysinfo_zj/hostname +ip folder;

    • Dump the file into ${sysinfo_dir} , the file will be stored in the /usr/appsoft/software/sysinfo_zj folder, under the "hostname +ip" The folder is empty.

      • Tips: In this way you need to pay attention to the remote host's search file with a special suffix: such as the file name + hostname , file name +ip, or multiple remote computer search back the same name file will be overwritten with each other, resulting in only the last acquisition of a computer file.

File search results show
    • Acquisition Method One

#将各计算机的hosts文件取回本地cp-a/etc/hosts ${sysinfo_get}

The file search results are:

[Email protected] software]# tree sysinfo_zj/sysinfo_zj/|--kylin3+192.168.101.207|   '--hosts|--log|--zj-5-2+192.168.101.202| '--hosts '--zj-6-2+192.168.101.206 '--hosts3 directories, 4 files
    • Acquisition Method II

#将各计算机的hosts文件取回本地cp-ar/etc/hosts ${sysinfo_dir}/hosts_${name}

The file search results are:

[Email protected] software]# tree sysinfo_zj/sysinfo_zj/|--hosts_kylin3|--hosts_zj-5-2|--hosts_zj-6-2|--kylin3+ 192.168.101.207|--log|--zj-5-2+192.168.101.202 '--zj-6-2+192.168.101.2063 directories, 4 files
Precautions
    • After editing the sysinfo-collect.sh script, select a remote host test script to run properly before performing batch configuration;

    • When you fill in the IP address range , please try to include as few as possible remote login (ping, SSH login not) device IP, otherwise it will reduce the efficiency of script execution.


This article from the "Future People" blog, reproduced please contact the author!

Linux System Bulk Configuration script

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.