脚本实现ssh免密登录

#!/bin/bash
hostnamectl set-hostname server3
infoarray=(“10.10.14.1x3|server3” “10.10.14.1x2|server2” “10.10.14.1x3|server1”)
hostsfile="/etc/hosts"
hostsname=cat /etc/hostname
hostip=ifconfig|grep "inet"|grep -v "127.0.0.1"|cut -d: -f2|awk '{print $2}'
#hostip=echo $hostip | awk 'BEGIN{ORS=" "}{print $0}'
#hostip=echo $hostip | awk '{gsub(/^\s+|\s+$/, "");print}'
#echo hostsip=KaTeX parse error: Expected 'EOF', got '#' at position 10: {hostip} #̲echo hostsname=hostsname

#replace chars
function replace_str()
{
str=$1
tstr=echo ${str/$2/$3}
echo $tstr
}

#insert info
function insert_info()
{
for i in ${infoarray[*]}
do
insertstr=replace_str $i "|" " "
eval sed -i "1i\$insertstr" /etc/hosts
done
}

#get hostname
function get_hostname()
{
str= 1 t s t r = 1 tstr= 1tstr={str##*|}
echo $tstr
}

#scp key
function scp_key()
{
cd /root/.ssh
ssh-keygen -t rsa

for i in ${infoarray[*]}
    do
    	hsname=`get_hostname $i`
	if [ "$hsname" != "$hostsname" ];then

echo $hsname

		ssh-copy-id root@${hsname}			
	fi	
    done

}

##################################################################
#set sshd_config
allowrootlogin=“PermitRootLogin yes”
sed -i “/#PermitRootLogin /c$allowrootlogin” /etc/ssh/sshd_config

systemctl restart ssh
insert_info
scp_key
systemctl restart ssh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值