linux 免密码执行脚本,Linux记录-ssh无密码执行脚本

#!/bin/bash

yum -y install sshpass

# confirm the user of the operation

echo "The current user is `whoami`"

# 1.generate the key pair

# 判断key是否已经存在,如果不存在就生成新的key

if [ -f ~/.ssh/id_rsa ];then

echo "rsa ssh-key file already exists" /bin/true

else

echo "rsa ssh-key file does not exists"

ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" >/dev/null 2>&1

if [ $? -eq 0 ];then

echo "generate rsa ssh-key" /bin/true

else

echo "generate rsa ssh-key" /bin/false

exit 1

fi

fi

# 2.distribution public key

for host in $(cat ./ssh-ip | grep -v "#" | grep -v ";" | grep -v "^$")

do

ip=$(echo ${host} | cut -f1 -d ":")

password=$(echo ${host} | cut -f2 -d ":")

user=root

port_ip=$(echo ${user}@${ip})

sshpass -p "${password}" ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no ${port_ip}

if [ $? -eq 0 ];then

echo "${ip} distribution public key" /bin/true

else

echo "${ip} distribution public key" /bin/false

exit 1

fi

done

vim ssh-ip

# 格式如下:

ip:密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值