linux配置ssh免密登陆

2018-03-31  本文已影响0人  默浑秩

通过执行该脚本,可以配置相应服务器ssh免密登陆

#!/bin/bash

Connect(){
    read -p "请输入需要统计信息的服务器IP:" IP
    read -p "请输入需要统计信息的服务器的ssh端口:" SSH_PORT
    Safe_Easy_SSH $IP $SSH_PORT
    read -p "继续配置请按1,退出请按0:" option
    if [ "$option" = "1" ]; then
    Connect 
    else    
        return 0
    fi
}
Safe_Easy_SSH(){
#$1 待连接客户端IP
    if test ! -e "/root/.ssh/id_rsa" ; then
        ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
    fi

    if test ! -e "/home/test_status/$IP.status" ; then
        #ssh-copy-id -i /root/.ssh/id_rsa $1
        ssh-copy-id -i /root/.ssh/id_rsa "-p $SSH_PORT $1"
        if [ "$?" = "0" ]; then
            mkdir -p /home/test_status
            echo ok >> "/home/test_status/$IP.status"
            echo "设置免密码登录$1成功。"
        else
            echo "设置免密码登录$1失败。"
            return 1
        fi
    fi
    return 0
}
Connect
上一篇下一篇

猜你喜欢

热点阅读