42 lines
1.1 KiB
Bash
42 lines
1.1 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Defining a list of SSH
|
|
# ssh_user,ssh_ip
|
|
# ssh_user,ssh_ip,ssh_port
|
|
SSH_LIST=(
|
|
"manager,172.16.5.100"
|
|
"manager,172.16.5.110"
|
|
"manager,172.16.5.120"
|
|
"manager,172.16.5.130"
|
|
"manager,172.16.5.140"
|
|
"manager,172.16.5.150"
|
|
"manager,172.16.5.160"
|
|
"manager,172.16.5.190"
|
|
)
|
|
DEFAULT_SSH_PORT=22
|
|
|
|
# Create .ssh directory if it doesn't exist
|
|
mkdir -p ~/.ssh
|
|
chmod 700 ~/.ssh
|
|
# Check if the SSH key already exists
|
|
if [ ! -f ~/.ssh/id_rsa ]; then
|
|
# Generate SSH key
|
|
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
chmod 644 ~/.ssh/id_rsa.pub
|
|
fi
|
|
|
|
for v in "${SSH_LIST[@]}"; do
|
|
IFS=',' read -r ssh_user ssh_ip ssh_port <<< "$v"
|
|
if [ -z "$ssh_port" ]; then
|
|
ssh_port=$DEFAULT_SSH_PORT
|
|
fi
|
|
echo "===> ${ssh_user} ${ssh_ip} ${ssh_port}"
|
|
|
|
# Copy public key content
|
|
cat ~/.ssh/id_rsa.pub | ssh -p ${ssh_port} ${ssh_user}@${ssh_ip} "\
|
|
mkdir -p ~/.ssh && chmod 700 ~/.ssh && \
|
|
touch ~/.ssh/authorized_keys && chmod 775 ~/.ssh/authorized_keys && \
|
|
cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
|
|
done
|