shell编程之【分发系统】

简介:

一、expect讲解


expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。


1、安装expect

[root@centos ~]# yum install -y expect


2、自动远程登入脚本

自动远程登录,并执行命令,下面介绍几个脚本:

第一个:登陆后不退出的脚本;

第二个:登陆后,执行命令然后退出的脚本;

第三个:传递参数登入,然后执行命令退出的脚本;

第四个:自动同步文件脚本;

第五个:指定host和要同步的文件。

1)登入后不退出的脚本

[root@centos ~]# cd /usr/local/sbin/

[root@centos sbin]# mkdir expect

[root@centos sbin]# cd expect/

[root@centos expect]# vim 1.expect

#! /usr/bin/expect

set host "192.168.0.10"

set passwd "123456"

spawn ssh  root@$host

expect {

"yes/no" { send "yes\r"; exp_continue}

"assword:" { send "$passwd\r" }

}

interact

[root@centos expect]# chmod a+x 1.expect      

[root@centos expect]# ./1.expect                 //运行脚本登入远程机器,logout就可以退出


2)执行命令后退出的脚本

[root@centos expect]# vim 2.expect

#!/usr/bin/expect

set user "root"

set passwd "123456"


spawn ssh $user@192.168.0.10


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@centos expect]# chmod a+x 2.expect      

[root@centos expect]# ./2.expect    


3)传递参数登入,执行命令后退出的脚本

[root@centos expect]# vim 3.expect

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "123456"

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"

注意:这里定义参数格式是 "$argv 0" ,和我们shell定义参数格式 "$0" 不太一样。cm定义后面需要执行的命令。

[root@centos expect]# chmod a+x 3.expect

[root@centos expect]# ./3.expect root 192.168.0.10 "cat /etc/passwd"


4)自动同步文件脚本

[root@centos expect]# vim 4.expect

#!/usr/bin/expect

set passwd "123456"

spawn rsync -avzP root@192.168.0.10:/tmp/12.txt /tmp/

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

注意:eof相当于结束的意思;另外要想实现远程传输文件,本机和远程机器都必须安装rsync。

[root@centos expect]# yum install -y rsync

[root@centos expect]# chmod a+x 4.expect

[root@centos expect]# ./4.expect


5)指定host和要同步的文件

[root@centos expect]# vim 5.expect

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -avzP $file root@$host:$file

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

[root@centos expect]# chmod a+x 5.expect

[root@centos expect]# ./5.expect 192.168.0.10 /tmp/12.txt

说明:要想实现同步,必须实现统一化,就是所有的远程机器密码相同,并且文件路径保持一致。这里的 $file 就是本机和远程机相同的文件路径,这样才能实现同步。若想实现多台的远程机器批量同步,我们进行下面操作:

[root@centos expect]# touch /tmp/ip.list                  //IP列表

[root@centos expect]# for ip in `cat /tmp/ip.list`; do echo $ip; ./5.expect $ip /tmp/12.txt; done



二、构建文件分发系统


    对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
    首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

1、批量分发文件

[root@centos ~]# cd /usr/local/sbin/

[root@centos sbin]# mkdir shell

[root@centos sbin]# cd shell/

[root@centos shell]# vim rsync.expect

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av --files-from=$file / root@$host:/

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

[root@centos shell]# vim rsync.sh

#!/bin/bash

for ip in `cat ip.list`

do

    echo $ip

    ./rsync.expect $ip file.list

done

[root@centos shell]# vim ip.list

192.168.0.115

192.168.0.114

[root@centos shell]# vim file.list                  //要确保每台机器都有下列文件(必须是绝对路径)

/123/test1.txt

/456/test2.txt

[root@centos shell]# chmod a+x rsync.expect

[root@centos shell]# chmod a+x rsync.sh

[root@centos shell]# ./rsync.sh                    //执行这个脚本实现文件同步


2、批量执行命令

[root@centos shell]# vim exe.expect

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "123456"

set cm [lindex $argv 1]


spawn ssh root@$host


expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

[root@centos shell]# vim exe.sh

#!/bin/bash

for ip in `cat ip.list`

do

    echo $ip

    ./exe.expect $ip "w;free -m;ls /tmp"

done

[root@centos shell]# chmod a+x exe.expect

[root@centos shell]# chmod a+x exe.sh

[root@centos shell]# ./exe.sh                  //执行这个脚本实现批量执行命令






      本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1712233,如需转载请自行联系原作者



相关文章
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
26天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
17 0
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
7天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
28 0
|
7天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
8天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
29 1
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
12天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
138 0
|
16天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
109 0
|
21天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0