1 Star 4 Fork 4

Operations/CDH安装

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ssh_key_copy.sh 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
Operations 提交于 2019-11-18 03:14 . update
#!/bin/bash
# 此脚本为批量部署服务器ssh key使用
#set -x
# check args count
if test $# -lt 3; then
echo -e "\nUsage: $0 < server ip > < username > < password > [ ssh port ]\n"
exit 1
fi
# install expect
[ -f /usr/bin/expect ] || yum install -y expect
server_list=$1
username=$2
password=$3
port=${4:-22}
# check sshkey file
sshkey_file=~/.ssh/id_rsa.pub
if ! test -e $sshkey_file; then
expect -c "
spawn ssh-keygen -t rsa
expect \"Enter*\" { send \"\n\"; exp_continue; }
"
fi
# get hosts list
hosts="$server_list"
echo "======================================================================="
echo "hosts: "
echo "$hosts"
echo "======================================================================="
ssh_key_copy()
{
# delete history
sed "/$1/d" -i ~/.ssh/known_hosts
# start copy
expect -c "
set timeout 100
spawn ssh-copy-id -p $port $username@$1
expect {
\"yes/no\" { send \"yes\n\"; exp_continue; }
\"password\" { send \"$password\n\"; }
\"already exist on the remote system\" { exit 1; }
}
expect eof
"
}
# auto sshkey pair
for host in $hosts; do
echo "======================================================================="
# check network
ping -i 0.2 -c 3 -W 1 $host >& /dev/null
if test $? -ne 0; then
echo "[ERROR]: Can't connect $host"
exit 1
fi
cat /etc/hosts | grep -v '^#' | grep $host >& /dev/null
if test $? -eq 0; then
hostaddr=$(cat /etc/hosts | grep -v '^#' | grep $host | awk '{print $1}')
hostname=$(cat /etc/hosts | grep -v '^#' | grep $host | awk '{print $2}')
ssh_key_copy $hostaddr
ssh_key_copy $hostname
else
ssh_key_copy $host
fi
echo ""
done
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/bookge/cdh_install.git
git@gitee.com:bookge/cdh_install.git
bookge
cdh_install
CDH安装
master

搜索帮助