【分发系统】yum -y install expect

#!/usr/bin/expect
set host "192.168.11.102"
set passwd ""
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

###############

#!/usr/bin/expect
set user "root"
set passwd ""
spawn ssh $user@192.168.11.18
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"

#################

#!/usr/bin/expect
set user [lindex $argv ]
set host [lindex $argv ]
set passwd ""
set cm [lindex $argv ]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

#######

#!/usr/bin/expect
set passwd ""
set host [lindex $argv ]
set file [lindex $argv ]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

执行: ./4.expect 192.168.11.18 /tmp/12.txt第二部分:构建文件分发系统1. 需求背景对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。2. 实现思路首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。3. 核心命令rsync -av --files-from=list.txt  /  root@host:/4. 文件分发系统的实现cat  rsync.expect

#!/usr/bin/expect
set passwd ""
set host [lindex $argv ]
set file [lindex $argv ]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
cat ip.list
192.168.11.18
192.168.11.19
......

cat rsync.sh

#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./rsync.expect $ip list.txt
done

5. 命令批量执行脚本cat exe.expect

#!/usr/bin/expect
set host [lindex $argv ]
set passwd ""
set cm [lindex $argv ]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

cat exe.sh

#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done

最新文章

  1. Ext3日记文件系统为什么文件系统还会损坏?
  2. vtigerCRM5.4的安装和汉化 ubuntu
  3. Qt之界面实现技巧
  4. <hr/> 水平线样式
  5. Linux下php5.3编译oracle客户端
  6. Angularjs之表单实例(三)
  7. markdownpad2 pro注册信息升级 破解版
  8. Java控制台输入ScannerDemo
  9. 如何构建高性能web网站:分布式缓存
  10. 两台主机之间单向Ping不通的问题
  11. 使用wget命令下载网络资源
  12. node path
  13. 目标检测:YOLO(v1 to v3)——学习笔记
  14. 我了解到的新知识之—MPLS
  15. javascript继承的6种方法
  16. ssh隧道的妙用
  17. 一些css属性的理解和运用。
  18. easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下
  19. MongoDB客户端及监控工具
  20. tomcat日志分类

热门文章

  1. vim全局替换命令-乾颐堂
  2. Greeplum 系列(四) 数据的装载与卸裁
  3. Google-优秀移动站点设计10招
  4. BAdi:BOM_UPDATE - Check update for BOM Save
  5. 常用数据库连接池 (DBCP、c3p0、Druid) 配置说明.RP
  6. mysql数据库引擎MyISAM与InnoDB的区别浅说
  7. 在Mac OS下配置PHP开发环境
  8. mybatis_入门程序
  9. mysql查询最近7天的数据,没有数据自动补0
  10. freemarker基本入门教程