#!/bin/bash
# 变量定义
source_host="192.168.42.250" # 源主机 IP
target_host="192.168.24.43" # 目标主机 IP
target_user="nvidia" # 目标主机的用户名
ssh_port="6666" # SSH 端口号
# 生成 SSH 密钥对(如果没有密钥对的话)
echo "生成 SSH 密钥对..."
if [ ! -f "$HOME/.ssh/id_rsa" ]; then
ssh-keygen -t rsa -b 2048 -f "$HOME/.ssh/id_rsa" -N ""
echo "SSH 密钥对生成成功!"
else
echo "SSH 密钥对已存在,跳过生成。"
fi
# 将公钥复制到目标主机(通过 SSH 登录目标主机)
echo "将公钥复制到目标主机 $target_host..."
ssh-copy-id -i "$HOME/.ssh/id_rsa.pub" -p $ssh_port $target_user@$target_host
# 测试免密登录
echo "测试免密登录..."
ssh -p $ssh_port $target_user@$target_host "echo '免密登录成功!'"
[root@bogon ziyong]#