一鍵SSH免密指令碼

2022-06-11 22:42:16 字數 2836 閱讀 9307

大型專案一鍵ssh免密指令碼

#!/usr/bin/env bash

root=$(

cd $(dirname $0)/..

pwd)

source $/dragonrc

read -p "請您輸入主機使用者:" user

if [ -z $user ];then

echo

echo -e "\033[1;32;31m警告:您輸入的使用者名為空!\033[0m"

echo

exit 0

firead -p "請您輸入主機密碼:" mima

if [ -z $mima ];then

echo

echo -e "\033[1;32;31m警告:您第一次輸入的密碼為空!\033[0m"

echo

exit 0

firead -p "請您再次輸入密碼:" queren

if [ -z $queren ];then

echo

echo -e "\033[1;32;31m警告:您第二次輸入的密碼為空!\033[0m"

echo

exit 0

fiif [ $mima == $queren ];then

echo "您輸入的密碼為:$queren" && sleep 1

echo "正在為您建立金鑰對兒,請您注意" && sleep 1

else

echo

echo -e "\033[1;32;31m不好意思~您兩次輸入的密碼不匹配,請您重新輸入!\033[0m"

echo

read -p "請您輸入主機使用者:" user

if [ -z $user ];then

echo

echo -e "\033[1;32;31m警告:您輸入的使用者名為空!\033[0m"

echo

exit 0

firead -p "請您輸入主機密碼:" mima

if [ -z $mima ];then

echo

echo -e "\033[1;32;31m警告:您第一次輸入的密碼為空!\033[0m"

echo

exit 0

firead -p "請您再次輸入密碼:" queren

if [ -z $queren ];then

echo

echo -e "\033[1;32;31m警告:您第二次輸入的密碼為空!\033[0m"

echo

exit 0

fiif [ $mima == $queren ];then

echo "您輸入的密碼為:$queren"

echo "正在為您建立金鑰對兒,請您注意" && sleep 1

else

echo

echo -e "\033[1;32;31m抱歉~您兩次輸入的密碼不匹配,請您重新執行指令碼!\033[0m"

echo

exit 1

fifi

> /root/.ssh/authorized_keys

if [ -f /root/.ssh/id_rsa.pub ];then

echo "您的主機已經有金鑰對兒了,不需要再次建立"

else

ssh-keygen -t rsa -p "" -f ~/.ssh/id_rsa

firpm -q sshpass &> /dev/null

if [ $? -ne 0 ];then

rpm -ivh $/rpm/other/sshpass-1.06-2.el7.x86_64.rpm &> /dev/null

echo "stricthostkeychecking no" > /root/.ssh/config

echo "userknownhostsfile=/dev/null" >> /root/.ssh/config

else

echo "stricthostkeychecking no" > /root/.ssh/config

echo "userknownhostsfile=/dev/null" >> /root/.ssh/config

fiifs=',' read -r -a all_nodes <<<"$"

for ip in "$"; do

sshpass -p $queren ssh-copy-id $user@$ip &> /dev/null

ssh $user@$ip "pwd" &> /dev/null

if [ $? -eq 0 ];then

echo "$ip is add successful"

else

echo "$ip is failure"

fidone

ansible $ -m shell -a "rm -rf /root/.ssh/id_rsa*"

ansible $ -m shell -a "ssh-keygen -t rsa -p \"\" -f ~/.ssh/id_rsa"

ifs=',' read -r -a nodes <<<"$"

for oip in "$"; do

ssh -t $user@$oip cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

done

ansible dragon -m copy -a "src=/root/.ssh/authorized_keys dest=/root/.ssh/authorized_keys"

echo -e "\033[1;32;31m雙向互信已完成!\033[0m"

SSH免密登入

a為本地主機 b為遠端主機 生成公鑰私鑰對 ssh keygen t rsa登入遠端機器 ssh root 然後輸入密碼 將.ssh的許可權設為700 mkdir ssh 如果資料夾已存在,跳過 chmod 700 ssh將本地生成的rsa公鑰複製到遠端機器 scp ssh id rsa.pub r...

SSH免密登入

當我們在配置多台計算,使之可以相互使用無密碼登入 ssh,之前都是一台一台的配置,現在一台a上新增b,然後在另一台b上再次新增a,這樣使得authorized keys中的內容相同,但時並不是完全相同,比如新增順序。如果超過2臺,這種配置就相當複雜了,而且容易出錯。後來,在網上看到了一種簡單的配置,...

SSH免密登入

前提 本地和遠端伺服器都已經安裝了ssh 遠端伺服器ip 192.168.1.145 遠端登入使用者 admin 本地命令 1 ssh keygen t rsa p 一路回車 2 scp ssh id rsa.pub admin 192.168.1.145 home admin ssh 3 按提示輸...