74.expect

分发系统-expect

yum install -y expect
  • 自动远程登陆
vim 1.expect
#! /usr/bin/expect
set host "192.168.133.132"  //变量设置,相似shell的 host=xx
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}  
"password:" { send "$passwd\r" }   //返回值匹配到yes/no时,输入yes并回车,返回值匹配到passwd时输入密码并回车
}
interact   //interact退出expect,留在远程主机;什么也不写会退出expect,并退出远程主机;expect eof 延迟几秒后退出expect脚本并退出远程主机
chomod a+x 1.expect
./1.expect  //执行1.expect脚本
  • 自动远程登陆后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132
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"  //退出终端,(退出expect,并退出远程主机)

74.expect

  • 传递参数($argv 0 第一个参数,依次类推)
#!/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"
./3.expect  root  192.168.127.134  "w;top;"

74.expect

  • 自动同步文件 (将另外一主机文件同步到本机器)
    vim 5.expect
    #!/usr/bin/expect
    set passwd "qwertyui"
    spawn rsync -av root@192.168.133.134:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect eof
    chmod a+x 5.expect
    ./5.expect
  • 指定host和要同步的文件
    vim 6.expect
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
chmod a+x  6.expect
./6.expect  192.168.127.134  /tmp/good.txt
  • 将本机的/tmp/good.txt传输到指定的主机

分发系统-构建文件分发系统

  • 需求背景对于大公司而言,确定时不时会有网站或者配置文件更新,并且使用的机器确定也是好多台,少则几台,多则几十甚至上百台。因此,自动同步文件是相当重要的。
  • 实现思路首先要有一台模板机器,把要分发的文件准备好,而后只要使用expect脚本批量把须要同步的文件分发到目标机器便可。
  • 核心命令rsync -av --files-from=list.txt  /  root@host:/
  • list.txt中是须要传输的文件的绝对路径列表
  • ip.list 是须要分发的主机ip
  • 文件分发系统的实现
  • rsync.expect 内容
#!/usr/bin/expect
set passwd "qwertyui"
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

ip.list内容
192.168.133.132
192.168.133.133
......shell

rsync.sh 内容vim

#!/bin/bash
for ip in `cat ip.list`
do
   echo $ip
   ./rsync.expect $ip list.txt
done
  • 将list.txt与ip.list 放在同一目录,执行sh脚本,即可以将文件分发到各服务器bash

  • 命令批量执行

exe.expect 内容服务器

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "qwertyui"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
set timeout -1  //永远不超时,expect默认10s超时,以后会自动断开,经过此项设置能够永不超时,此项设置只能够加在send命令以后
expect "]*"
send "exit\r"
chmod a+x exe.expect
./exe.expect 192.168.127.134 top

exe.sh 内容ssh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done
  • 执行sh脚本即可以批量执行命令
相关文章
相关标签/搜索