20.27 Distribution System Introduction
Distribution system, in what scenarios
For example, companies are doing more and more business. The programming language of their website or App is PHP, so to run this environment, PHP code needs to configure the environment of LAMP or LNMP, and code needs to be uploaded to the server, which is a website. In normal work, business is iterating and new functions appear.If there are more than a dozen or even hundreds of machines, the storage is a site. For example, if there is an interface, App has a large amount of access, App needs to call the server interface, which has 50 machines to host.At this point, we need to have a distribution system that allows us to publish code that is updated every day or every time to 50 machines.
Tools that can be brought online by shell programming
Description: The distribution system, also known as the online shell script
The core thing is called expect, expect is also a scripting language, much like shell. We can use expect to transfer files. We can also execute commands remotely without entering passwords. The so-called online code is to publish the developed language or code to an online environment.
In the distribution system here, you first have to prepare a template machine. The code on this machine is the latest code, which is the code to be brought online. Then, for example, you have to bring 50 machines online to know the IP address of these 50 machines and the corresponding user passwords of these 50 machines. Finally, you use expect script to push these codes to these 50 machines with the help of rsnc.Machine.
20.28 Expct script remote login
yum install expect
[root@root-01 ~]# yum install -y expect
Write expect script--Automatic remote login
#Automatic Remote Login Script [root@root-01 test]# vim 1.expect #! /usr/bin/expect #host is a variable followed by the IP you logged in to set host "192.168.2.116" #passwd is a variable followed by the password for IP set passwd "123456" #Logon statement spawn ssh root@$host #When you sign in for the first time, you will be prompted to continue expect { "yes/no" {send"yes\r";exp_continue} "password:"{send "$passwd\r"} } interact //Note: \r --> means to return after interact --> Indicates that you need to stay on a remote machine and exit the remote script if you do not add this parameter //Or use expect EOF --> to indicate that you log on to a remote machine, stay for a second or two, and then exit automatically # Add Execution Rights [root@root-01 test]# chmod a+x 1.expect #implement [root@root-01 test]# ./1.expect spawn ssh root@192.168.2.116 The authenticity of host '192.168.2.116 (192.168.2.116)' can't be established. ECDSA key fingerprint is bd:fd:56:cf:07:80:0d:a6:9c:38:00:be:33:1f:f5:7a. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.2.116' (ECDSA) to the list of known hosts. root@192.168.2.116's password: Last login: Wed Sep 20 10:25:06 2017 from 192.168.2.102 [root@root-02 ~]# //Description: Successfully logged on to 192.168.2.116 machine
20.29 Expct script remote execution command
After automatic remote login -- execute commands and exit
[root@root-01 test]# vim 2.expect #! /usr/bin/expect #user is a variable set user "root" #passwd is a variable followed by the user's password set passwd "123456" #Logon statement spawn ssh $user@192.168.2.116 #When you sign in for the first time, you will be prompted to continue expect { "yes/no" {send "yes/r"; exp_continue} "password:" {send "$passwd\r"} } expect "]*" send "touch /tmp/66.txt\r" expect "]*" send "echo This is test > /tmp/66.txt\r" expect "]*" send "exit\r" //Note: expect']*'When the root user logs in, the second half of the parentheses are followed by # ([root@root-01 ~]#) //When an average user logs in, the second half of the brackets are followed by ~. expect "]*" Use here*Sign, indicating a wildcard, whether it is root User or normal user will execute the following command #Add Execution Rights [root@root-01 test]# chmod a+x 2.expect #implement [root@root-01 test]# ./2.expect spawn ssh root@192.168.2.116 root@192.168.2.116's password: Last login: Wed Sep 20 10:30:43 2017 from 192.168.2.115 [root@root-02 ~]# touch /tmp/66.txt [root@root-02 ~]# echo This is test > /tmp/66.txt [root@root-02 ~]# [root@root-01 test]# //Description: Log on to root-02, execute two commands, and exit #You can execute 1.expect to log in to root-02 and check to see if you created 66.txt [root@root-01 test]# ./1.expect spawn ssh root@192.168.2.116 root@192.168.2.116's password: Last login: Wed Sep 20 11:08:09 2017 from 192.168.2.115 [root@root-02 ~]# ls /tmp/66.txt /tmp/66.txt [root@root-02 ~]# cat /tmp/66.txt This is test //Description: There are 66.txt under root-02/tmp/and there is also "This is test" indicating successful execution.
20.30 expect script pass parameters
Pass-through parameters
[root@root-01 test]# vim 3.expect #! /usr/bin/expect #user first parameter set user [lindex $argv 0] #Second host parameter set host [lindex $argv 1] set passwd "123456" #The third parameter of cm, the command to execute set cm [lindex $argv 2] spawn ssh $user$@host expect { "yes/no" {send "yes\r"} "password:" {send "$passwd\r"} } expect "]*" send "$cm\r" expect "]*" send "exit\r" #Add Execution Rights [root@root-01 test]# chmod a+x 3.expect #implement [root@root-01 test]# ./3.expect root 192.168.2.116 ls spawn ssh root@192.168.2.116 root@192.168.2.116's password: Last login: Wed Sep 20 11:12:05 2017 from 192.168.2.115 [root@root-02 ~]# ls anaconda-ks.cfg [root@root-02 ~]# [root@root-01 test]# //Description: root is the first parameter; 192.168.2.116 is the second parameter; ls is the third parameter; #When you need to execute more than one command, you need to enclose the command with a colon and semicolon [root@root-01 test]# ./3.expect root 192.168.2.116 "ls;touch 1.txt;echo "this is test" > 1.txt" spawn ssh root@192.168.2.116 root@192.168.2.116's password: Last login: Wed Sep 20 11:39:34 2017 from 192.168.2.115 [root@root-02 ~]# ls;touch 1.txt;echo this 1.txt anaconda-ks.cfg this //Description: touch 1.txt, and write this is test #Execute 1.expect to log in to root-02 to see if touch 1.txt and what is written [root@root-01 test]# ./1.expect spawn ssh root@192.168.2.116 root@192.168.2.116's password: Last login: Wed Sep 20 11:40:25 2017 from 192.168.2.115 [root@root-02 ~]# cat 1.txt this is test [root@root-02 ~]# ls 1.txt anaconda-ks.cfg //Description: Clearly successful execution