多台机器建立ssh pub key登录方式的脚本

目的

公司内部有很多台Linux机器需要维护,每次ssh登录都要输入密码,非常麻烦。因此偷个懒,写了一个脚本,运行该脚本之后,下次再连接就不需要输入密码了。

使用提示

使用该脚本之前,请务必使用ssh-keygen生成公钥。
目前这个脚本运行时还需要输入多次密码,这是不足之处。

#!/bin/bash

# Declare an array of string with type
declare -a hostList=(
"192.168.129.3"
"192.168.129.5"
"192.168.129.7"
"192.168.129.11"
"192.168.129.12"
)

passwd=mypasswd

set -x

setSSH() {
for host in ${hostList[@]}; do
    echo "===========set ssh login without passwd on host $host ==========="
    echo ${passwd}  | ssh  -o StrictHostKeyChecking=no -tt hicode@$host  'mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys'  && cat ~/.ssh/id_rsa.pub | ssh hicode@$host 'cat >> .ssh/authorized_keys'
done
}

setSSH

你可能感兴趣的:(多台机器建立ssh pub key登录方式的脚本)