我有一个脚本可以从/etc/hosts 文件中读取主机 IP,执行 ssh 并在那里为给定用户更新密码。但是,脚本在为第一台主机设置密码后关闭,无法为其余主机设置密码。请找到以下文件-
/etc/hosts
文件-
172.x.x.x host1
172.x.x.x host2
172.x.x.x host3
我正在使用的脚本是-
key=$1
user=$2
password=$3
filename='/etc/hosts'
while IFS= read -r line; do
IFS=' ' read -r -a array <<< $line
ssh -i $key centos@${array[0]} "echo "$password" | sudo passwd $user --stdin"
done <$filename
脚本很简单。但是在从文件中为 host1
设置密码后它正在关闭。有谁知道我错过了什么?
最佳答案
默认情况下,ssh 从您的输入文件 stdin 读取。ssh 消耗文件的其余部分并且您的 while 循环终止。因此请尝试使用
ssh -n
关于linux - 在脚本中使用 ssh 执行 passwd 命令后没有返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908536/