歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> CentOS 7 Expect + Shell實現批量創建文件

CentOS 7 Expect + Shell實現批量創建文件

日期:2017/2/28 13:43:40   编辑:SHELL編程

如何在CentOS 7 Expect + Shell實現批量創建文件,這個問題好久了,一直沒整理出來。記得還是上次面試的時候問到如何批量修改1000台機器的主機名。這裡給出批量創建內容為Hello,名為1.txt的文件。

一、首先安裝expect

yum install -y expect

二、准備工作:(1)准備IP地址的列表。vim ip.txt

192.168.1.88

192.168.1.89

192.168.1.90

(2)准備要執行的命令。vim cmd.txt

mkdir /tmp/test

三、腳本部分

#!/bin/bash

passwd="123456"
sc=$(cat /tmp/cmd.txt)

echo $sc
cat ip.txt |while read line
do
/usr/bin/expect <<EOF
set timeout 10
spawn ssh root@$line
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" {send "$passwd\r"}

}
expect "]#"

send "$sc\r"
send "exit\r"
expect eof
EOF
done
exit 0

局限:假如密碼不同,此方案需要另行考慮。可能會加上一個passwd的文件。

Copyright © Linux教程網 All Rights Reserved