这个问题好久了,一直没整理出来。记得还是上次面试的时候问到如何批量修改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 catip.txt|whilereadline do /usr/bin/expect<<EOF settimeout10 spawnsshroot@$line expect{ "yes/no"{send"yes\r";exp_continue} "password:"{send"$passwd\r"} } expect"]#" send"$sc\r" send"exit\r" expecteof EOF done exit0