#!/usr/bin/expect set passwd "123456" spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
#!/usr/bin/expect set passwd "123123a" 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
一、rsync.expect 內容shell
#!/usr/bin/expect set passwd "123123a" 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 list.txt donebash
這個sh 的目的,就是遍歷一下 ip列表文件中的 ip地址多線程
最重要的,expect腳本 必須加入執行權限ssh
文件不存在,會報錯網站
分發系統還有一個重要的關鍵是,確保同步的機器的密碼一致,不然將不能實現同步;因此這就存在一個弊端,一旦腳本暴露,將會讓別人知道如何登錄你機器;固然也有對應的解決辦法,那就是使用密鑰認證,這樣的話,天然在命令行業省去「輸入密碼< password:" { send "$passwd\r" } >''」和「定義密碼< set passwd "123123a" >」的命令了spa
#!/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
shell多線程.net