Eighteen weekly classes

Eighteen weekly classes

20.27 Introduction to the distribution system
20.28 Remote login with
expect script 20.29 Remote command execution with
expect script 20.30 Passing parameters in expect script

20.27 Introduction to Distribution System

Introduction to the distribution system

  • Distribution system -expect explanation (that is, a distributed script)
  • Scenes:
    • The business is getting bigger and bigger, the website app, back-end, and the programming language are php, so you need to configure lamp or lnmp, and it is best to upload the code to the server; but because the business increases, the code increases, and multiple machines will be used. Very troublesome; it only requires a distribution system to publish the code of each update to the server that needs to be updated
  • expect, is a scripting language; through it, you can realize transmission and input commands (online code)
  • First, prepare a template machine, the IP of the machine, the password of the corresponding user, synchronize the code through rsync, and execute some commands through expect

20.28 expect script remote login

expect script remote login

  • yum install -y expect

  • Automatic remote login

[root@aminglinux-02 sbin]# vim 1.expect

#! /usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
spawn ssh root@$host   
expect {
"yes/no" { send "yes\r"; exp_continue}  //
"password:" { send "$passwd\r" }   
}
interact  //脚本结束
  • Define variables in expect, use set
  • This file is to ensure the login information. If it is empty, re-login to ssh remotely will prompt /root/.ssh/known_hosts
    exp_continue means continue\r means newline interact and continue to stay on this machine without exiting

Add execute permission

[root@aminglinux-02 sbin]# ./1.expect
-bash: ./1.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 1.expect
  • successfully logged in
[root@aminglinux-02 sbin]# ./1.expect
spawn ssh [email protected]
[email protected]'s password:
Last failed login: Wed Sep 20 23:31:03 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Sep 20 22:29:52 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ^C

20.29 expect script to execute commands remotely

expect script to execute commands remotely

  • After manually logging in remotely, execute the command and exit
#!/usr/bin/expect
set user "root"
set passwd "123123a"
spawn ssh [email protected]

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}

expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
  • expect "] " : " " wildcard all characters to the right of ],

  • send execute command

  • A machine executes the script

[root@aminglinux-02 sbin]# ./2.expect
-bash: ./2.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 2.expect
[root@aminglinux-02 sbin]# ./!$
./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 20 23:31:49 2017 from 192.168.133.131
[root@aminglinux-01 ~]# touch /tmp/12.txt
[root@aminglinux-01 ~]# echo 1212 > /tmp/12.txt
  • Machine B checks whether the command run by the script is successful
[root@aminglinux-01 ~]# ls /tmp/
12.txt      systemd-private-7723d189c8d3469ab732edadbbc340cb-nginx.service-eKHAIK
mysql.sock  systemd-private-7723d189c8d3469ab732edadbbc340cb-vmtoolsd.service-1yxB4M
[root@aminglinux-01 ~]# cat /tmp/12.txt
1212


20.30 expect script to pass parameters

expect script to pass parameters

  • pass parameters
#!/usr/bin/expect
set user [lindex $argv 0]   //第一个参数
set host [lindex $argv 1]   //第二个参数
set passwd "123123a"
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"
  • ./3.expect [first argument] [second argument] [third argument]
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 ls
spawn ssh [email protected]
[email protected]'s password:
Last failed login: Thu Sep 21 01:57:01 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Sep 21 01:51:46 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ls
anaconda-ks.cfg
  • Support multiple parameters
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 "ls;whoami;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 21 01:57:21 2017 from 192.168.133.131
[root@aminglinux-01 ~]# ls;whoami;vmstat 1
anaconda-ks.cfg
root
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0      0 1470532    876 189616    0    0    12     2   42   93  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   41   91  0  1 99  0  0
0  0      0 1470532    876 189616    0    0     0     0   33   74  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   37   83  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   35   77  0  0 100  0  0
^C
  • Because vmstat 1 is running continuously. So there is no way to execute the last exit of the script, it can only be terminated manually

Friendly link: A Ming linux

Guess you like

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