shell 脚本 批量实现主机免密登录

脚本功能

实现了批量导入公钥到对方主机,实现ssh免密登录。修改了/etc/ssh/ssh_config中的StrictHostKeyChecking 不检查对方公钥,第一次连接的时候,便不会问你是否保存公钥

#!/bin/bash
#
#********************************************************************
#Author:		liuhao
#QQ: 			1921160095
#Date: 			2019-11-06
#FileName:		ip.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
read -p "请输入起始ip: " beginip
read -p "请输入结束ip: " endip
read -p "请输入密码: " mima
begarray=(${beginip//./ })
endarray=(${endip//./ })
if [ "${begarray[0]}" = "${endarray[0]}" -a "${begarray[1]}" = "${endarray[1]}" -a "${begarray[2]}" = "${endarray[2]}" ]; then
        [ -f /root/.ssh/id_rsa ] || ssh-keygen -P "" -f /root/.ssh/id_rsa &> /dev/null
	rpm -q sshpass &> /dev/null || yum install sshpass -y &> /dev/null
	sed -i '/Checking ask/c StrictHostKeyChecking no' /etc/ssh/ssh_config
	begin=${begarray[3]}
	end=${endarray[3]}
	while [ "${begarray[3]}" -le "${endarray[3]}" ];do
		if ping -c1 -w1 ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} &> /dev/null ; then
		{
			if sshpass -p $mima ssh-copy-id -i  /root/.ssh/id_rsa.pub ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} &> /dev/null ;then
				echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} success >> success.txt
			else
				echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} error >> error.txt
			fi
			echo ${begarray[3]}
		}&
		else
			echo  ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} host is unreachable >> error.txt
		fi
		let begarray[3]++
	done
	wait
else
	echo 仅支持变动最后一位,范围1~254
fi 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值