當業務越作越大,服務器需求愈來愈多,幾臺服務器的話還好一點;當十幾、幾十臺的時候,工做量就很是大!而且不規範,須要一個模板機分發到各個機器上去。
能夠用開源的軟件,expect腳本語言,進行實現分發系統的功能。mysql
[root@zlinux-01 ~]# yum install -y expect //安裝 [root@zlinux-01 sbin]# vim 01.expect //自動遠程登陸,並執行命令 #!/usr/bin/expect set host "192.168.242.129" #遠程機器IP set passwd "rootroot" spawn ssh root@$host expect { "yes/no" {send "yes\r"; exp_continue} "assword:" {send "$passwd\r"} } interact #表示停留在機器上 #若是須要退出 能夠expect eof [root@zlinux-01 sbin]# chmod a+x 01.expect //授予執行權限 您在 /var/spool/mail/root 中有新郵件 [root@zlinux-01 sbin]# ./01.expect //執行 spawn ssh root@192.168.242.129 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)? yes Warning: Permanently added '192.168.242.129' (ECDSA) to the list of known hosts. root@192.168.242.129's password: Last login: Wed Apr 25 21:37:48 2018 from 192.168.242.1
[root@zlinux-01 sbin]# vim 02.expect //創建腳本 #!/usr/bin/expect set user "root" set passwd "rootroot" spawn ssh $user@192.168.242.129 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" [root@zlinux-01 sbin]# chmod a+x 02.expect [root@zlinux-01 sbin]# ./02.expect spawn ssh root@192.168.242.129 root@192.168.242.129's password: Last login: Wed Apr 25 21:43:13 2018 from 192.168.242.128 [root@zlinux-02 ~]# touch /tmp/12.txt [root@zlinux-02 ~]# echo 1212 > /tmp/12.txt [root@zlinux-02 ~]# [root@zlinux-01 sbin]#
#遠程客戶端上查看是否成功建立文件 [root@zlinux-02 ~]# ls /tmp/ 12.txt mysql.sock systemd-private-152865e96f164fe4b06027db92130672-vmtoolsd.service-sxTKDs [root@zlinux-02 ~]# cat /tmp/12.txt 1212
[root@zlinux-01 sbin]# vim 03.expect #!/usr/bin/expect set user [lindex $argv 0] set host [lindex $argv 1] set passwd "rootroot" 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" [root@zlinux-01 sbin]# chmod a+x 03.expect [root@zlinux-01 sbin]# ./03.expect root 192.168.242.129 ls spawn ssh root@192.168.242.129 root@192.168.242.129's password: Last login: Wed Apr 25 23:31:11 2018 from 192.168.242.128 [root@zlinux-02 ~]# ls anaconda-ks.cfg [root@zlinux-02 ~]# [root@zlinux-01 sbin]# ./03.expect root 192.168.242.129 "ls;ps aux | grep mysql" spawn ssh root@192.168.242.129 root@192.168.242.129's password: Last login: Wed Apr 25 23:32:12 2018 from 192.168.242.128 [root@zlinux-02 ~]# ls;ps aux | grep mysql anaconda-ks.cfg root 798 0.0 0.0 115380 1704 ? S 14:00 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zlinux-02.pid mysql 1227 0.0 24.4 1300788 456724 ? Sl 14:00 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=3306 root 2588 0.0 0.0 112664 972 pts/1 S+ 23:32 0:00 grep --color=auto mysql [root@zlinux-02 ~]# [root@zlinux-01 sbin]# ./03.expect root 192.168.242.129 "ls;ps aux | grep nginx" spawn ssh root@192.168.242.129 root@192.168.242.129's password: Last login: Wed Apr 25 23:32:47 2018 from 192.168.242.128 [root@zlinux-02 ~]# ls;ps aux | grep nginx anaconda-ks.cfg root 2609 0.0 0.0 112664 972 pts/1 S+ 23:33 0:00 grep --color=auto nginx