【分发系统】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

expect分发脚本的更多相关文章

  1. (转)expect命令脚本语言介绍及生产实践

    原文:http://www.fblinux.com/?p=526 Expect介绍 expect是一个用来实现自动交互功能的软件套件,是用来实现自动和交互式任务程序进行通信,无需人的手工干预.比如SS ...

  2. ssh无密登录_集群分发脚本xsync

    1.ssh免密登录 ssh ip地址 [root@192 ~]# ssh 192.168.1.102 root@192.168.1.102's password: Last login: Mon Fe ...

  3. Shell 分发脚本

    目录 Shell分发脚本 原理 rsync命令分析 特点 基本语法 实现 需求 环境变量 脚本实现 知识点 获得当前路径的目录dirname 获得当前路径的文件名basename shell远程执行命 ...

  4. expect 分发ssh key脚本

    #!/usr/bin/expect } { send_user "USAGE:expect_sshkey.exp host" exit } #define var set host ...

  5. linux文件分发脚本

    1.说明 此脚本可分发两类文件,1.固定内容文件,2.(每台被分发主机)内容不同的文件 ppp.sh为拨号脚本,每台被分发主机内容不同 根据分发文件名字不同(ppp.sh和其他文件)自动选择分发方式 ...

  6. SCCM2012分发脚本

    1.分发批处理脚本 命令行:script.bat 2.分发PowerShell脚本 命令行:PowerShell.exe -executionpolicy unrestricted -file .\s ...

  7. Expect 小脚本

    简介: Expect 可以替系统管理员完成与系统的交互式操作 shell > yum -y install expect # 可以通过 yum 安装 shell > which expec ...

  8. linux shell脚本中使用expect(脚本打开新终端自动远程连接顺便输一点指令)(巨坑)

    放弃吧 我找了六个小时都没找到可以用的方案(指标题括号里的内容) 给个曲线救国的方法: 现把expect脚本写成一个文件 在另一个shell脚本中调用

  9. ssh秘钥免交互批量分发脚本

    将以下内容保存为.sh文件后运行即可,需根据各自情况修改ip_up和ip_arr #!/bin/bash #脚本功能:ssh秘钥免交互批量分发 #制 作 人:罗钢 联系方式:278554547@qqc ...

随机推荐

  1. spring源码学习——spring整体架构和设计理念

    Spring是在Rod Johnson的<Expert One-On-One J2EE Development and Design >的基础上衍生而来的.主要目的是通过使用基本的java ...

  2. Loadrunner11无法在win7 64位上启用ie解决办法

    Loadrunner11无法在win7 64位上启用ie解决办法 1.loadrunner11在win7 64位上默认启用的是32位的那个IE浏览器,路径:C:\Program Files (x86) ...

  3. input框添加阴影效果

    .input:hover,.input:focus{ border:1px solid #85afe1; -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0 ...

  4. 斐波那契数列—java实现

    最近在面试的时候被问到了斐波那契数列,而且有不同的实现方式,就在这里记录一下. 定义 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

  5. [operator]ELK6 index pattern的问题

    完成了EL/FK的搭建之后,在kibana的主页只能看到默认的索引? 其实这个索引名字的设置是在logstash-smaple.conf(elk6.4)里的设置,比如我这样设置 input { bea ...

  6. ubuntu 16.04启用root和ssh登录

    1.设置用户密码 sudo passwd root 2.vim /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 末尾添加:greeter-show-m ...

  7. java Long、Integer 、Double、Boolean类型 不能直接比较

    测试: System.out.println(new Long(1000)==new Long(1000)); System.out.println(new Integer(1000)==new In ...

  8. log4j.properties加入内容

    log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender. ...

  9. 从swing分发线程机制上理解多线程[转载]

    本文参考了 http://space.itpub.net/13685345/viewspace-374940,原文作者:javagui 在多线程编程当中,总会提到图形编程,比如java中的swing, ...

  10. tcxgrid控件中drag a column header here to group by that column移除方法