shell编程之【分发系统】
2015-11-12 16:36
627 查看
一、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
[root@centos expect]# chmod a+x 1.expect
[root@centos expect]# ./1.expect //运行脚本登入远程机器,logout就可以退出
2)执行命令后退出的脚本
[root@centos expect]# vim 2.expect
[root@centos expect]# chmod a+x 2.expect [root@centos expect]# ./2.expect
3)传递参数登入,执行命令后退出的脚本
[root@centos expect]# vim 3.expect
注意:这里定义参数格式是 "$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
注意: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
[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
[root@centos shell]# vim rsync.sh
[root@centos shell]# vim ip.list
[root@centos shell]# vim file.list //要确保每台机器都有下列文件(必须是绝对路径)
[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
[root@centos shell]# vim exe.sh
[root@centos shell]# chmod a+x exe.expect
[root@centos shell]# chmod a+x exe.sh
[root@centos shell]# ./exe.sh //执行这个脚本实现批量执行命令
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]# ./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" |
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" |
[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 |
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]# 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 |
#!/bin/bash for ip in `cat ip.list` do echo $ip ./rsync.expect $ip file.list done |
192.168.0.115 192.168.0.114 |
/123/test1.txt /456/test2.txt |
[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" |
#!/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.sh
[root@centos shell]# ./exe.sh //执行这个脚本实现批量执行命令
相关文章推荐
- android wifi 无线调试
- 运维入门
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Linux Shell常用技巧
- Shell 脚本编程陷阱
- shell字符串操作详解
- Shell中删除某些文件外所有文件的3个方法
- Ruby中执行Linux shell命令的六种方法详解
- VB使用shell函数打开外部exe程序的实现方法
- Shell编程的一些知识集合
- Shell中的for和while循环详细总结
- 什么是Shell?Shell脚本基础知识详细介绍
- Shell脚本中引用、调用另一个脚本文件的2种方法
- Shell脚本解压rpm软件包
- Linux Shell 数组建立及使用技巧
- Shell脚本实现复制文件到多台服务器的代码分享
- Shell脚本实现批量替换文件内容
- Shell脚本实现的一个简易Web服务器例子分享
- linux Shell学习笔记第五天