Shell批量SSH免交互登录认证

简介: Shell批量SSH免交互登录认证

脚本实现功能:批量或单个SSH免交互登录认证

脚本应用场景:当部署集群时,大多数实现要配置好管理节点与从节点的SSH免交互登录,针对这样的情况,写了下面脚本,简化工作。

脚本支持系统:Ubuntu和CentOS

#!/bin/bash
# Description: configuration local host and remote host ssh keypair authentication, Support Ubuntu and CentOS operation system.
# Blog: http://lizhenliang.blog.51cto.com
 
function color_echo() {
    if [ $1 == "green" ]; then
        echo -e "\033[32;40m$2\033[0m"
    elif [ $1 == "red" ]; then
        echo -e "\033[31;40m$2\033[0m"
    fi
}
function os_version() {
    local OS_V=$(cat /etc/issue |awk 'NR==1{print $1}')
    if [ $OS_V == "\S" -o $OS_V == "CentOS" ]; then
        echo "CentOS"
    elif [ $OS_V == "Ubuntu" ]; then
        echo "Ubuntu"
    fi
}
function check_ssh_auth() {
    if $(grep "Permission denied" $EXP_TMP_FILE >/dev/null); then
        color_echo red "Host $IP SSH authentication failure! Login password error."
        exit 1
    elif $(ssh $INFO 'echo yes >/dev/null'); then
        color_echo green "Host $IP SSH authentication successfully."
    fi
    rm $EXP_TMP_FILE >/dev/null
}
function check_pkg() {
    local PKG_NAME=$1
    if [ $(os_version) == "CentOS" ]; then
        if ! $(rpm -ql $PKG_NAME >/dev/null 2>&1); then
            echo no
        else
            echo yes
        fi
    elif [ $(os_version) == "Ubuntu" ]; then
        if ! $(dpkg -l $PKG_NAME >/dev/null 2>&1); then
            echo no
        else
            echo yes
        fi
    fi
}
function install_pkg() {
    local PKG_NAME=$1
    if [ $(os_version) == "CentOS" ]; then
        if [ $(check_pkg $PKG_NAME) == "no" ]; then
            yum install $PKG_NAME -y
            if [ $(check_pkg $PKG_NAME) == "no" ]; then
                color_echo green "The $PKG_NAME installation failure! Try to install again."
                yum makecache
                yum install $PKG_NAME -y
                [ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit 1
            fi
        fi
    elif [ $(os_version) == "Ubuntu" ]; then
        if [ $(check_pkg $PKG_NAME) == "no" ]; then
            apt-get install $PKG_NAME -y
            if [ $(check_pkg $PKG_NAME) == "no" ]; then
                color_echo green "$PKG_NAME installation failure! Try to install again."
                apt-get autoremove && apt-get update
                apt-get install $PKG_NAME --force-yes -y
                [ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit 1
            fi
        fi
    fi
}
function generate_keypair() {
    if [ ! -e ~/.ssh/id_rsa.pub ]; then
        color_echo green "The public/private rsa key pair not exist, start Generating..."
        expect -c "
            spawn ssh-keygen
            expect {
                \"ssh/id_rsa):\" {send \"\r\";exp_continue}
                \"passphrase):\" {send \"\r\";exp_continue}
                \"again:\" {send \"\r\";exp_continue}
            }
        " >/dev/null 2>&1
        if [ -e ~/.ssh/id_rsa.pub ]; then
            color_echo green "Generating public/private rsa key pair successfully."
        else
            color_echo red "Generating public/private rsa key pair failure!"
            exit 1
        fi
    fi
}

EXP_TMP_FILE=/tmp/expect_ssh.tmp

if [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@.* ]]; then
    install_pkg expect ; generate_keypair
    for i in $@; do
        USER=$(echo $i|cut -d@ -f1)
        IP=$(echo $i|cut -d@ -f2)
        PASS=$(echo $i|cut -d@ -f3)
        INFO=$USER@$IP
        expect -c "
            spawn ssh-copy-id $INFO
            expect {
                \"(yes/no)?\" {send \"yes\r\";exp_continue}
                \"password:\" {send \"$PASS\r\";exp_continue}
            }
        " > $EXP_TMP_FILE  # if login failed, login error info append temp file
        check_ssh_auth
    done
elif [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}@.* ]]; then
    install_pkg expect ; generate_keypair
    START_IP_NUM=$(echo $1|sed -r 's/.*\.(.*)-(.*)@.*/\1/')
    END_IP_NUM=$(echo $1|sed -r 's/.*\.(.*)-(.*)@.*/\2/')
    for ((i=$START_IP_NUM;i<=$END_IP_NUM;i++)); do
        USER=$(echo $1|cut -d@ -f1)
        PASS=$(echo $1|cut -d@ -f3)
        IP_RANGE=$(echo $1|sed -r 's/.*@(.*\.).*/\1/')
        IP=$IP_RANGE$i
        INFO=$USER@$IP_RANGE$i
        expect -c "
            spawn ssh-copy-id $INFO
            expect {
                \"(yes/no)?\" {send \"yes\r\";exp_continue}
                \"password:\" {send \"$PASS\r\";exp_continue}
            }
        " > $EXP_TMP_FILE
        check_ssh_auth
    done
else
    echo "Example1: $0 <root@192.168.1.10-15@password>"
    echo "Example2: $0 <root@192.168.1.10@password>"
    echo "Example3: $0 [root@192.168.1.10@password root@192.168.1.11@password root@192.168.1.12@password ...]"
fi

wKioL1admPGjUzpHAACA5MDjsdg283.gif

wKiom1admYKhH0pBAAA9234_Rgg754.gif


相关文章
|
2月前
|
Ubuntu Shell 网络安全
安装了ubuntu虚拟机后发现shell无法连接 ubuntu开启ssh连接
【8月更文挑战第23天】安装了ubuntu虚拟机后发现shell无法连接
116 6
|
2月前
|
JavaScript 应用服务中间件 Linux
【应用服务 App Service】解决无法从Azure门户SSH登录问题
【应用服务 App Service】解决无法从Azure门户SSH登录问题
|
2月前
|
机器学习/深度学习 存储 Linux
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
|
2月前
|
安全 Shell Linux
如何禁止某个用户使用ssh登录
本文介绍了五种禁止用户通过SSH登录的方法:1) 修改`/etc/ssh/sshd_config`文件中的`DenyUsers`和`DenyGroups`来阻止特定用户或用户组登录;2) 将用户的默认shell设置为`/usr/sbin/nologin`或`/bin/false`以禁用其SSH访问;3) 利用PAM(可插入认证模块)通过编辑`/etc/security/sshd.conf`来限制登录权限;4) 通过编辑`/etc/hosts.deny`文件拒绝特定用户的SSH访问;5) 锁定或禁用用户账号以阻止所有类型的登录。每种方法都提供了详细的步骤指导。
84 1
|
2月前
|
存储 安全 测试技术
【超实用却暗藏杀机】sshpass:一键免密SSH登录的神器,为何生产环境却要敬而远之?探秘背后的安全隐患与替代方案!
【8月更文挑战第16天】sshpass 是一款便捷工具,可实现自动化SSH登录,简化脚本中的远程连接流程。通过后台自动处理密码输入,便于执行远程操作,如 `sshpass -p &#39;yourpassword&#39; ssh user@remotehost`。也可结合更多SSH选项使用,例如指定私钥文件。然而,因需明文传递密码,存在较大安全隐患,不适于生产环境;推荐使用公钥认证以增强安全性。
53 4
|
2月前
|
安全 Linux Shell
Linux系统之间实现免密码登录(SSH无密码登录
【8月更文挑战第21天】要在Linux系统间实现SSH免密码登录,需先在源机器生成SSH密钥对,然后将公钥复制到目标机器的`.ssh/authorized_keys`文件中。可通过`ssh-keygen`命令生成密钥,并使用`ssh-copy-id`命令传输公钥。最后测试SSH连接,确保能无密码登录。若目标机器缺少相关目录或文件,需手动创建并设置适当权限。完成这些步骤后,即可实现安全便捷的免密码登录。
56 0
|
2月前
|
Ubuntu Linux 网络安全
在Linux中,如何禁用root用户直接SSH登录?
在Linux中,如何禁用root用户直接SSH登录?
|
3月前
|
安全 Linux 网络安全
|
3月前
|
Shell 网络安全 数据安全/隐私保护
MacOS Sonoma14.2.1系统SSH免密登录
【7月更文挑战第9天】在MacOS Sonoma 14.2.1中设置SSH免密登录,包括:1) 使用`ssh-keygen`生成RSA密钥对;2) 使用`ssh-copy-id`将公钥传到远程主机;3) 用`ssh-add --apple-use-keychain`添加私钥到ssh-agent,并为重启后自动添加配置自动化脚本;4) 可选地,编辑`~/.ssh/config`设置别名简化登录。确保远程主机的`.ssh/authorized_keys`文件权限为600。
122 4
|
2月前
|
存储 安全 Linux
说到Linux安全,SSH限制IP登录绕不开这3种方法!
说到Linux安全,SSH限制IP登录绕不开这3种方法!
下一篇
无影云桌面