命令和
腳本,重啟服務,重啟服務器等,如果人工去一臺臺操作的話會特別繁瑣,并浪費人力。
這時我們可以使用expect,向目標服務器上發送指令去實現批量操作。
下面的例子將在
centos上將一個文件,批量拷貝到其他服務商上,并執行相應的
命令
1. 在centos上安裝expect
yum install expect
2. 編寫expect
腳本 copyfilebatch.sh
下面的腳本將向內網IP為 192.168.0.102 至 192.168.0.112 的服務器分別拷貝一個rc.local文件,拷貝成功后,執行chmod命令,分別重啟服務器
#!/usr/bin/expect -f
set password rootpassword
for {set i 102} {$i <= 112} {incr i} {
set ip "192.168.0.$i"
puts "$ip"
spawn ssh -o StrictHostKeyChecking=no $ip
set timeout 3
expect "root@$ip's "
set timeout 3
send "$password\\\\r"
set timeout 3
send "exit\\\\r"
spawn scp /home/install/rc.local root@$ip:/etc/rc.d/rc.local
set timeout 3
expect "root@$ip's "
set timeout 3
send "$password\\\\r"
set timeout 3
send "exit\\\\r"
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\\\\r"; exp_continue}
"*" { send "$password\\\\r" }
}
expect "#*"
#要執行的命令
send "chmod x /etc/rc.d/rc.local\\\\r"
send "reboot\\\\r"
send "exit\\\\r"
expect eof
}
以上就是本文的全部內容,希望對大家的學習有所幫助
原文來自:
https://www.linuxprobe.com/expect-batch-command.html
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn