expect 小知识 需要转义的字符 \ 需转义为 \
} 需转义为 } [ 需转义为 [ $ 需转义为 \$ 需转义为 \
" 需转义为 \" #!/bin/bash user='root' password_dir=/mnt #cmd="echo 'hahah' > /mnt/test.txt" ip=cat $password_dir/password.txt|awk '{print $1}'
password=grep $ip password.txt |awk '{print $2}'
ssh() { expect -c " set timout 60
spawn ssh $user@$ip expect { "yes/no" {send "yes\r";exp_continue;} "assword:" {send $password\r;} } expect
sleep 10 expect "]" send "echo 'hahah' > /mnt/test.txt\r"
interact #########允许用户交互 ##如果不需要登录进入交互可以忽略
expect eof"
}
for ip in $ip do ssh done