Centos rapid deployment (reproduced)

Reprinted file:
http://lpyyn.iteye.com/blog/2260897

Scenario: You need to deploy the same project package on N servers.
Goal: Use centos commands or scripts to interact with remote machines, copy deployment packages, stop or start servlet containers.
Tools:
Expect: A software suite for automating interactive tools (Expect [is a] software suite for automating interactive tools). Using it, system administrators can create scripts to provide input to commands or programs that expect input from a terminal, which generally requires manual input. Expect is a scripting language based on the Tcl language. In fact, whether it is an interactive or non-interactive application, Expect can show its talents, but for specific interactive occasions, it is none other than Except.
NFS: Unified storage of deployment files. Disadvantage: After the nfs host is down, all other servers cannot access project files. Rarely happens in centos, but for stability use scp to copy deployment files to each server.
Implementation:
Install
Java code Collection code
#Install tcl 
yum -y install tcl 
yum -y install tcl-devel 
#Install expect 
yum -y install expect 
New sh script file


#!/bin/bash
name1="Jack"
echo $name1

Java代码  收藏代码
#!/usr/bin/expect 
set timeout 30  
spawn ssh -l root 172.16.230.108 
expect "password:" 
#replace ispassword with real password 
send "ispassword\r" 
#expect *# character , and input next command 
expect "*#" 
#send command ,  '\r' equal press enter key 
send "cd /test/apache-tomcat-7.0.65 \r" 
expect "*#" 
send "bin/shutdown.sh \r" 
expect "*#" 
send "bin/startup.sh & tail -f logs/catalina.out \r" 
#wait for log output  
interact 
#execut logout , return to the invoker 

spawn: It is an internal command of expect that can be executed after entering the expect environment. If expect is not installed or executed directly under the default SHELL, the spawn command cannot be found. So don't use commands like "which spawn" to find the spawn command. For example, dir in windows is an internal command. This command is provided by the shell. You cannot find an executable file of dir.com or dir.exe. Its main function is to add a shell to the ssh running process to pass interactive instructions.
#!/usr/bin/expect : This line tells the operating system which shell to use for the code in the script to execute. The expect here is actually the same thing as bash under linux and cmd under windows.
expect "password:" : expect here is also an internal command of expect. The shell command of expect is the same as the internal command, but it is not a function, just get used to it. The meaning of this command is to judge whether the string "password:" is contained in the last output result. If so, return immediately, otherwise wait for a period of time and return. The waiting time here is the 30 seconds set earlier.
interact : Keep the interactive state after the execution is completed, and hand over the control to the console. At this time, it can be operated manually. If you do not have this sentence, you will log out after logging in instead of staying on the remote terminal.

SCP reference original text: http://www.cnblogs.com/hitwtx/archive/2011/11/16/2251254.html
Linux cp/scp command + scp command detailed 

name: cp

permission to use: all users How

to use :

cp [ options] source dest

cp [options] source... directory

Description: Copy a file to another file, or copy several files to another directory.

The program-a

will copy the file status, permissions and other information as it is as much as possible.

-r If the source contains a directory name, the files in the directory will be copied to the destination in sequence.

-f If a file with the same file name already exists at the destination, delete it before copying.

Example:

Copy the file aaa (existing) and name it bbb:

cp aaa bbb

Copy all C language programs to the Finished subdirectory:

cp *.c Finished

command: scp

copy files between different Linux are commonly used in 3 Method:

The first is ftp, that is, one of the Linux installations ftp Server, so that the other can use the ftp client program to copy files.

The second method is to use the samba service, which is similar to the Windows file copy operation, which is more concise and convenient.

The third is to use the scp command to copy files.

    scp is a file copy with Security, based on ssh login. It is more convenient to operate. For example, to copy the current file to another remote host, the following command can be used.

scp /home/daisy/full.tar.gz [email protected]:/home/root

Then you will be prompted to enter the login password of the root user of the other 172.19.2.75 host, and then start copying.

    If you want to do it in reverse, copying files from the remote host to the current system is also very simple.

Linux cp/scp command + scp command detailed explanation (transfer) .gz

linux's scp command can copy files and directories between linux;

=================
scp command
============== ====
scp can copy files between 2 linux hosts;

basic command format:
       scp [optional parameters] file_source file_target

======
copy from local to remote
======
* copy files:
        * command Format:
                scp local_file remote_username@remote_ip:remote_folder
                or
                scp local_file remote_username@remote_ip:remote_file
                Or
                scp local_file remote_ip:remote_folder
                or
                scp local_file remote_ip:remote_file

                The first and second specify the user name. After the command is executed, you need to enter the password again. The first one only specifies the remote directory, and the file name remains unchanged. The second specifies File name;
                the 3rd and 4th ones do not specify the user name, the user name and password are required after the command is executed, the third one only specifies the remote directory, the file name remains unchanged, and the fourth one specifies the file name;
        * Example:
                scp / home/space/music/1.mp3 [email protected]:/home/root/others/music
                scp /home/space/music/1.mp3 [email protected]:/home /root/others/music/001.mp3
                scp /home/space/music/1.mp3 www.cumt.edu.cn:/home/root/others/music
                scp /home/space/music/1.mp3 www. cumt.edu.cn:/home/root/others/music/001.mp3

* Copy directory:
        * Command format:
                scp -r local_folder remote_username@remote_ip:remote_folder
                or
                scp -r local_folder remote_ip:remote_folder

                The first one specifies the user name, and you need to enter the password after the command is executed;
                the second one does not specify the user name, and you need to enter the command after the command is executed. Username and password;
        * Example:
                scp -r /home/space/music/ [email protected]:/home/root/others/
                scp -r /home/space/music/ www.cumt.edu. cn:/home/root/others/

                The above command copies the local music directory to the remote others directory, that is, there is a remote one after copying../others/music/ directory


======
Copy from remote to local
=== ===
To copy from remote to local, just reverse the order of the last 2 parameters of the command to copy from local to remote;

for example:
        scp [email protected]:/home/root/others/music /home/space/music/1.mp3
        scp -r www.cumt.edu.cn:/home/root/others/ /home/

The simplest application of space/music/ is as follows:

scp local user name@IP address: file name 1 remote user name@IP address: file name 2

[local user name@IP address:] can not be entered, it may be necessary to enter the remote user name The corresponding password.

Several parameters that may be useful:

-v Same as -v in most linux commands, used to display progress. Can be used to view connection, authentication, or configuration errors.

-C Enable compression option.

-P selects the port. Note that -p has been used by rcp.

-4 forcibly uses the IPV4 address.

-6 forcibly uses the IPV6 address.



Note two points:
1. If the remote server firewall has special restrictions, scp will use a special port. What port depends on the situation, the command format is as follows:
#scp -p 4588 [email protected]:/usr/local/sin.sh /home/administrator
2. When using scp, pay attention to whether the user you are using has readable Get the permissions of the corresponding file on the remote server.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940075&siteId=291194637