shell 生成public key,實現免密碼SSH登錄

#!/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!'那說明設置成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章