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腳本即可以批量執行命令
相關文章
相關標籤/搜索