linux - 在脚本中使用 ssh 执行 passwd 命令后没有返回

标签 linux bash ssh while-loop

我有一个脚本可以从/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/

相关文章:

c - 从 C 中的队列中删除项目的线程

linux - 如何在多核系统上获取 CPU 使用率

shell - 在命令行上 merge Mercurial 冲突?

ssh - Vagrant 卡住连接超时重试

linux - shm_open 未设置组写访问权限

linux - 如何在未配置的 Centos 7 Docker 上删除/安装 docker 镜像

bash - 如何响应 shell 脚本中的密码提示?

linux - bash 通配符 n 位

linux - 使用打开进程的时间保存变量

java - 使用 Jsch 的 .keystore 文件中的私有(private) RSA key