while嵌套expect实现免交互执行命令

1、添加用户tom100

#!/bin/bash
while read LINES
do
        user=`echo $LINES |cut -d ' ' -f1`
        password=`echo $LINES |cut -d ' ' -f2`
        host=`echo $LINES |cut -d ' ' -f3`
        expect <<EOF
        spawn ssh $user@$host
        expect "yes/no"  {send "yes\r"}
        expect "password:" {send "$password\r"}
        expect "#" {send "useradd tom100\r"}
        send "exit\r"
        expect eof
EOF
done <ip.txt

2、ip.txt的格式

root redhat 192.168.101.20
root redhat 192.168.101.21
root redhat 192.168.101.22

猜你喜欢

转载自www.cnblogs.com/hym-by/p/12744316.html