-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-copy-id.sh
More file actions
47 lines (44 loc) · 1.5 KB
/
ssh-copy-id.sh
File metadata and controls
47 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# 设置SSH登录的密码
password="matrixai@2"
hosts_file="hosts"
# 检查是否存在SSH公钥,如果不存在则生成
if [ ! -f ~/.ssh/id_rsa ]; then
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
# 检查是否存在目标文件
if [ ! -f "$hosts_file" ]; then
echo "hosts文件不存在或无法访问"
exit 1
fi
#Windows下复制粘贴到hosts文件的回车和Linux不是一种字符,格式转换,非常重要
sed -i 's/\r$//' $hosts_file
#检测是否安装了sshpass
if [ ! -x "$(command -v sshpass)" ]; then
# echo "sshpass未安装"
# echo "请使用以下命令安装sshpass"
# echo "sudo apt-get install sshpass"
# exit 2
echo "sshpass未安装,正在安装..."
sudo apt-get install sshpass
echo "sshpass安装完成"
fi
# 逐行读取hosts文件并检查是否为IP格式
while IFS= read -r line || [[ -n "$line" ]]; do
# 使用正则表达式检查每行是否是IP地址
if [[ $line =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
#echo "IP地址: $line"
echo "正在分发SSH公钥到 $line"
# 使用ssh-copy-id分发SSH公钥,自动回答yes和输入密码
sshpass -p "$password" ssh-copy-id -o StrictHostKeyChecking=no "$line" &>/dev/null
if [ $? -eq 0 ]; then
echo "成功分发SSH公钥到 $line"
echo
else
echo "分发SSH公钥到 $line 失败"
echo
fi
# else
# echo "不符合IP地址格式的行: $line"
fi
done < "$hosts_file"