shell脚本基础(七)

一、分发系统介绍

当业务越做越大,服务器需求越来越多,几台服务器的话还好一点;当十几、几十台的时候,工作量就非常大!并且不规范,需要一个模板机分发到各个机器上去。
可以用开源的软件,expect脚本语言,进行实现分发系统的功能。

二、expect脚本远程登录

[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 [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)? yes
Warning: 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

三、expect自动远程登录后执行命令并退出

[root@zlinux-01 sbin]# vim 02.expect      //建立脚本

#!/usr/bin/expect
set user "root"
set passwd "rootroot"
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"

[root@zlinux-01 sbin]# chmod a+x 02.expect
[root@zlinux-01 sbin]# ./02.expect 
spawn ssh [email protected]
[email protected]'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

四、expect脚本传递参数

[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 [email protected]
[email protected]'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 [email protected]
[email protected]'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 [email protected]
[email protected]'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

猜你喜欢

转载自blog.51cto.com/3069201/2107900
今日推荐