centos rapid deployment

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.

tool:

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 a specific interactive occasion, 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.

accomplish:

 Install

#install tcl
yum -y install tcl
yum -y install tcl-devel
#install expect
yum -y install expect

 Create a new sh script file

 

#!/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, where the waiting time is the 30 seconds set earlier.

interactKeep 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

Detailed explanation of cp/scp command + scp command of Linux  

 

name: cp

Access: All users

How to use:

cp [options] source dest

cp [options] source... directory

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

count

-a Copy the file status, permissions, etc. as they are 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 (which already exists) and name it bbb:

cp aaa bbb

Copy all C programs to the Finished subdirectory:

cp *.c Finished

Command: scp

There are three common ways to copy files between different Linux:

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 root@172.19.2.75:/home/root

然后会提示你输入另外那台172.19.2.75主机的root用户的登录密码,接着就开始copy了。

    如果想反过来操作,把文件从远程主机copy到当前系统,也很简单。

linux之cp/scp命令+scp命令详解(转) - linmaogan - 独木★不成林 scp root@/full.tar.gz  172.19 . 2.75 : / home / root /full.tar.gz  home / daisy / full . tar . gz

linux 的 scp 命令 可以 在 linux 之间复制 文件 和 目录; 

================== 
scp 命令 
================== 
scp 可以在 2个 linux 主机间复制文件; 

命令基本格式: 
       scp [可选参数] file_source file_target 

====== 
从 本地 复制到 远程 
====== 
* 复制文件: 
        * 命令格式: 
                scp local_file remote_username@remote_ip:remote_folder 
                或者 
                scp local_file remote_username@remote_ip:remote_file 
                或者 
                scp local_file remote_ip:remote_folder 
                或者 
                scp local_file remote_ip:remote_file 

                第1,2个指定了用户名,命令执行后需要再输入密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名; 
                第3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名; 
        * 例子: 
                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 

* 复制目录: 
        * 命令格式: 
                scp -r local_folder remote_username@remote_ip:remote_folder 
                或者 
                scp -r local_folder remote_ip:remote_folder 

                第1个指定了用户名,命令执行后需要再输入密码; 
                第2个没有指定用户名,命令执行后需要输入用户名和密码; 
        * 例子: 
                scp -r /home/space/music/ [email protected]:/home/root/others/ 
                scp -r /home/space/music/ www.cumt.edu.cn:/home/root/others/ 

                上面 命令 将 本地 music 目录 复制 到 远程 others 目录下,即复制后有 远程 有 ../others/music/ 目录 


====== 
从 远程 复制到 本地 
====== 
从 远程 复制到 本地,只要将 从 本地 复制到 远程 的命令 的 后2个参数 调换顺序 即可; 

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

最简单的应用如下 : 

scp 本地用户名 @IP 地址 : 文件名 1 远程用户名 @IP 地址 : 文件名 2 

[ 本地用户名 @IP 地址 :] 可以不输入 , 可能需要输入远程用户名所对应的密码 . 

可能有用的几个参数 : 

-v 和大多数 linux 命令中的 -v 意思一样 , 用来显示进度 . 可以用来查看连接 , 认证 , 或是配置错误 . 

-C 使能压缩选项 . 

-P 选择端口 . 注意 -p 已经被 rcp 使用 . 

-4 强行使用 IPV4 地址 . 

-6 强行使用 IPV6 地址 .

 

注意两点:
1.如果远程服务器防火墙有特殊限制,scp便要走特殊端口,具体用什么端口视情况而定,命令格式如下:
#scp -p 4588 [email protected]:/usr/local/sin.sh /home/administrator
2.使用scp要注意所使用的用户是否具有可读取远程服务器相应文件的权限。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260913&siteId=291194637