The problem has been a long time, and has not been sorted out. Remember the last interview when asked how to bulk modify the host name of 1000 machines. Here is a file named 1.txt that is created in bulk for hello.
First, install the expect
Yum Install-y expect
Ii. preparatory work: (1) Prepare a list of IP addresses. Vim Ip.txt
192.168.1.88
192.168.1.89
192.168.1.90
(2) Prepare the command to be executed. Vim Cmd.txt
Mkdir/tmp/test
Third, the script part
#!/bin/bashpasswd= "123456" sc=$ (cat/tmp/cmd.txt) echo $sccat ip.txt |while Read Linedo/usr/bin/expect <<eofset Timeout 10spawn ssh [email protected] $lineexpect {"yes/no" {send "yes\r"; exp_continue} "password:" {send "$passwd \ r"}}ex Pect "]#" send "$SC \ r" Send "exit\r" expect Eofeofdone exit 0
Limitations: If the password is different, this scenario needs to be considered separately. A passwd file may be added.
This article is from the "Share,open source" blog, so be sure to keep this source http://liqilong2010.blog.51cto.com/3029053/1875114
CentOS 7 expect + Shell implementation batch file creation