需求
批量實現自動發送公鑰到遠程主機
環境
firewalld:關閉
selinux:關閉
實現方式
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批量自動發送公鑰到遠程主機,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
新聞熱點
疑難解答