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,如需转载请自行联系原作者



相关文章
|
11天前
|
关系型数据库 MySQL Shell
备份 MySQL 的 shell 脚本(mysqldump版本)
【4月更文挑战第28天】
23 0
|
1天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
1天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
2天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
20 5
|
3天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
3天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
3天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
12 3
|
9天前
|
弹性计算 运维 监控
|
9天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
10 0
|
10天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形3
【4月更文挑战第29天】
21 0