小编给大家分享一下shell脚本如何实现ssh-copy-id批量自动发送公钥到远程主机,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
需求
批量实现自动发送公钥到远程主机
环境
firewalld:关闭
selinux:关闭
实现方式
sshpass命令
shell调用expect命令
sshpass命令
#!/bin/bash##********************************************************************#Author: hechunping#QQ: ×××#Date: 2019-11-07#FileName: ssh-sshpass.sh#URL: hexiaoshuai.blog.51cto.com#Description: The test script#Copyright (C): 2019 All rights reserved#********************************************************************NET=172.20.200USER=(root hechunping)PASSWORD=123456ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/nullsed -i '/StrictHostKeyChecking/c StrictHostKeyChecking no' /etc/ssh/ssh_configrpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/nullfor i in {1..254} ; do{ sshpass -p $PASSWORD ssh-copy-id -i ${USER[0]}@${NET}.${i} &> /dev/null}&donewait
shell调用expect命令
#!/bin/bash##********************************************************************#Author: hechunping#QQ: ×××#Date: 2019-11-07#FileName: ssh-expect.sh#URL: hexiaoshuai.blog.51cto.com#Description: The test script#Copyright (C): 2019 All rights reserved#********************************************************************NET=172.20.200USER=(root hechunping)PASSWORD=123456ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &> /dev/nullfor i in {1..254} ; do{expect <<EOFspawn ssh-copy-id -i ${USER[0]}@${NET}.${i}expect { "yes/no" { send "yes\n";exp_continue } "password" { send "${PASSWORD}\n" }}expect eofEOF}&donewait
看完了这篇文章,相信你对“shell脚本如何实现ssh-copy-id批量自动发送公钥到远程主机”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网行业资讯频道,感谢各位的阅读!