Introduction of distribution System
When the business is bigger, the server needs more and more, a few servers is a bit better, when more than 10, dozens of, the workload is very large! And not specification, need a template machine to distribute to each machine up.
You can use open source software, expect scripting language, to implement the functions of the distribution system.
Second, expect script remote login
[[email protected] ~]# yum install -y expect //安装[[email protected] sbin]# vim 01.expect //自动远程登录,并执行命令#!/usr/bin/expectset host "192.168.242.129"#远程机器IPset passwd "rootroot"spawn ssh [email protected]$hostexpect { "yes/no" {send "yes\r"; exp_continue} "assword:" {send "$passwd\r"}}interact #表示停留在机器上#如果需要退出 可以expect eof[[email protected] sbin]# chmod a+x 01.expect //授予执行权限您在 /var/spool/mail/root 中有新邮件[[email protected] sbin]# ./01.expect //执行spawn ssh [email protected]The authenticity of host ‘192.168.242.129 (192.168.242.129)‘ can‘t be established.ECDSA key fingerprint is 22:fb:63:5d:8c:78:4e:74:99:f7:b1:b3:a3:70:8d:d3.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘192.168.242.129‘ (ECDSA) to the list of known hosts.[email protected]‘s password: Last login: Wed Apr 25 21:37:48 2018 from 192.168.242.1
Third, expect automatic telnet after executing the command and exit
[[email protected] sbin]# vim 02.expect
#远程客户端上查看是否成功创建文件[[email protected] ~]# ls /tmp/12.txt mysql.sock systemd-private-152865e96f164fe4b06027db92130672-vmtoolsd.service-sxTKDs[[email protected] ~]# cat /tmp/12.txt 1212
Iv. Expect script pass parameters
[[email protected] sbin]# vim 03.expect#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "Rootroot" set cm [lindex $argv 2]spawn ssh [email protected] $hostexpect {"yes/no" {send "yes\r"} "Password:" {send " $PASSWD \ r "}}expect"]* "send" $cm \ r "expect"]* "send" exit\r "[[email protected] sbin]# chmod a+x 03.expect[[email& Nbsp;protected] sbin]#./03.expect root 192.168.242.129 lsspawn ssh [email protected][email protected] ' s Password:last login:wed Apr 23:31:11 2018 from 192.168.242.128[[email protected] ~]# LSANACONDA-KS.CFG[[EMAIL&N Bsp;protected] ~]# [[email protected] sbin]#./03.expect root 192.168.242.129 "ls;ps aux | grep mysql "spawn ssh [email protected][email protected] ' s password:last login:wed Apr 23:32:12 2018 from 19 2.168.242.128[[email protected] ~]# ls;ps aux | grep mysqlanaconda-ks.cfgroot 798 0.0 0.0 115380 1704? S-0:00/bin/sh/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql--pid-file=/data/mysql/zlinux-02.pidmysql 1227 0.0 24.4 1300788 456724? Sl 0:18/usr/local/mysql/bin/mysqld--basedir=/usr/local/mysql--datadir=/data/mysql--plugin-dir=/usr/local/ Mysql/lib/plugin--user=mysql--log-error=/data/mysql/zlinux-02.err--pid-file=/data/mysql/zlinux-02.pid--socket= /tmp/mysql.sock--port=3306root 2588 0.0 0.0 112664 972 PTS/1 s+ 23:32 0:00 grep--color=auto Mysql[[emai L protected] ~]# [[email protected] sbin]#./03.expect root 192.168.242.129 "ls;ps aux | grep nginx "Spawn ssh [email protected][email protected] ' s password:last login:wed Apr 23:32:47 2018 from 19 2.168.242.128[[email protected] ~]# ls;ps aux | grep nginxanaconda-ks.cfgroot 2609 0.0 0.0 112664 972 PTS/1 s+ 23:33 0:00 grep--color=auto nginx
Shell Script Basics (vii)