#!/bin/bash
if [ ! $1 ]; then
echo "usage: copyssh.sh user@remoteserver "
exit
fi
PORT=22
if [ $2 ]; then
PORT=$2
fi
echo $1 $PORT
# Uploads your id_rsa.pub to the specified host, wrapped for readability
if [ ! -r ${HOME}/.ssh/id_rsa.pub ]; then
ssh-keygen -b 2048 -t rsa
fi
# Make sure auth file exists and chmod to 600
ssh $1 -p $PORT 0> echo "mkdir ~/.ssh; touch ~/.ssh/authorized_keys;
chmod u+rw .ssh/authorized_keys"
# Append to the copy on the remote server
cat ~/.ssh/id_rsa.pub | ssh $1 -p $PORT "cat - >> .ssh/authorized_keys"
if [ $? -eq 0 ]; then
echo "Success"
fi使用方法:
1.保存成文件 copyssh.sh
2.然后在shell >. ./copyssh.sh username@remoteserver
3.输入密码两次,出现 'success!'那说明设置成功。
本文介绍了一个简单的bash脚本,用于自动化SSH密钥的生成及远程服务器上的部署过程。用户只需通过命令行调用该脚本并提供远程服务器的信息,即可轻松完成SSH免密码登录的设置。
1102

被折叠的 条评论
为什么被折叠?



