批量传公钥后可以使用ansible批量修改ssh登录策略
将主机名(或ip)与登录密码对应放在一个文件中,中间以空格隔开,用户名默认root,如有不同请按需修改
如: host1 password1
host2 password2
!/bin/bash
HELP(){
echo “Usage: $(basename $0) “
echo “此脚本用于将公钥批量传递到host-inventory中记录的所有在线的主机中”
}
[[ “$1” == “-h” || “$1” == “–help” ]] && HELP && exit
[ ! -f “$1” ] && echo “错误, 请查看帮助信息并检查host-inventory文件!” && exit 1
echo -e “请确认\e[32mKaTeX parse error: Undefined control sequence: \e at position 4: {1}\̲e̲[0m中的格式为\e[31mI…cho” =~ 1$ ]] && echo “已取消” && exit
if ! [ -x /usr/bin/expect ];then
yum install -y expect &> /dev/null
! [ -x /usr/bin/expect ] && echo “安装 expect 失败” && exit 1
fi
expect << eof
spawn ssh-keygen
expect “.ssh/id_rsa):”
send “\n”
expect {
“(empty for no passphrase):” {
send “\n”
expect “same passphrase again:”
send “\n”
}
“Overwrite (y/n)?” { send “n\n” }
}
expect eof
eof
file=$1
user=root
while read line;do
host=$(awk ‘{print $1}’ <<< $line) # 如果资产文件格式不同,注意修改此处 ping -c1 KaTeX parse error: Expected 'EOF', got '&' at position 6: host &̲> /dev/null || …(awk ‘{print $2}’ <<< $line) # 如果资产文件格式不同,注意修改此处
expect <<- eof
spawn ssh-copy-id
u
s
e
r
@
user@
user@host
expect {
“continue connecting (yes/no)?” { send “yes\n”; exp_continue }
“password:” { send “$pass\n” }
}
expect eof
eof
done < $file
Yy ↩︎