美文网首页
Linux系统root用户无密码访问脚本(ssh)

Linux系统root用户无密码访问脚本(ssh)

作者: sxqiong | 来源:发表于2017-12-23 11:20 被阅读72次

    半年前写的脚本,等有时间就加注释(滑稽)

    前言

    脚本无非就是写给电脑的记事本,记事本上告诉电脑你要干什么(买二斤白菜,一斤猪肉,俩馒头·······),所以在写脚本前要清楚自己要做什么,才能给计算机描述清楚,前端后端皆是如此。

    手动实现无密码访问

    先手动的方式来实现无密码访问,再去研究如何写脚本。

    SSH验证:从客户端来看,SSH提供两种级别的安全验证。

    第一种级别(基于口令的安全验证)

    只要你知道自己帐号和口令,就可以登录到远程主机。所有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的攻击。

    第二种级别(基于密匙的安全验证)

    需要依靠密匙,也就是你必须为自己创建一对密匙,并把公用密匙放在需要访问的服务器上。如果你要连接到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在该服务器上你的主目录下寻找你的公用密匙,然后把它和你发送过来的公用密匙进行比较。如果两个密匙一致,服务器就用公用密匙加密“质询”(challenge)并把它发送给客户端软件。客户端软件收到“质询”之后就可以用你的私人密匙解密再把它发送给服务器。

    用这种方式,你必须知道自己密匙的口令。但是,与第一种级别相比,第二种级别不需要在网络上传送口令。

    说白了一种场景是:

    • 服务器A: 密码是什么
    • 服务器B:******

    一种场景是:

    • 服务器A:天王盖地虎
    • 服务器B:宝塔镇河妖

    而我们要做的就是第二种校验。
    实现步骤
    (因为要做root无密码访问,所以所有执行全在root下,普通用户原理是一样的,不过是认证文件位置不同罢了,sudo su -sudo su的区别大家都知道的吧。

    • 首先执行ssh-keygen -t rsa生成密钥对
    • 将密钥拷贝至需要无密码访问的机器scp id_rsa.pub(key访问加-i)
    • 去无密码访问的机器将拷贝来的密钥写入授权列表cat id_rsa.pub >> /root/.ssh/authorized_keys
    • 文件授权chmod 600 authorized_keys 至于为什么是这个权限忘记了
    • 上述步骤中拷贝key的地方可替换为ssh-copy-id -i /root/.ssh/id_rsa.pub

    上脚本文件

    脚本需要服务器安装expect命令,atp-get或yum install就行,需要bash来执行入口文件

    • 入口文件startAuthorize.sh
    #!/bin/bash
    path=$(cd "$(dirname "$0")"; pwd)
    sshPath="/root/.ssh/authorized_keys"
    if [ ! -f "$sshPath" ]; then
    echo "密钥不存在,正在生成密钥"
    expect productKey.exp
    cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
    echo "密钥生成完成,正在修改服务器配置"
    else
    echo "密钥已经存在,正在修改服务器配置"
    fi
    
    for i in `awk '{print $1}' ${path}/pwdconfig`
    do
    u=`awk -v I="$i" '{if(I==$1)print $2}' ${path}/pwdconfig`
    p=`awk -v I="$i" '{if(I==$1)print $3}' ${path}/pwdconfig`
    rp=`awk -v I="$i" '{if(I==$1)print $4}' ${path}/pwdconfig`
    echo "开始修改ip为 $i 的服务器配置"
    expect ${path}/initServer.exp $i $p $u $rp >/dev/null 2>&1
    if [ "$?" = 0 ]; then
    echo "正在为ip为 $i 的服务器授权"
    expect ${path}/authorize.exp $i $rp >/dev/null 2>&1
    if [ "$?" = 0 ]; then
    echo "服务器 $i 授权成功"
    fi
    else
    echo "\033[31m无法为ip为 $i 的服务器进行授权 请重新修改服务器配置 \033[0m"
    fi
    done
    echo "所有服务器配置修改完成"
    sh ${path}/confirmServer.sh
    
    • 生成密钥productKey.exp
    #!/bin/expect
    set timeout 30
    spawn -noecho ssh-keygen -t rsa
    expect {
        "Enter file in which to save the key" {
            send "\r"
            expect {
                            "Overwrite" {
                                    send "y\r"; exp_continue;
                             }
                    "Enter passphrase" {
                        send "\r"
                        expect "Enter same passphrase again:"
                        send "\r"
                    }
            }
        }
    }
    expect eof
    
    • 初始化服务器(启用root用户脚本)initServer.exp
    #!/bin/expect
    set serverIp [lindex $argv 0]
    set passwd [lindex $argv 1]
    set user [lindex $argv 2]
    set rootPasswd [lindex $argv 3]
    set timeout 30
    spawn -noecho ssh -l $user $serverIp
    expect {
        "yes/no" { send "yes\r"; exp_continue}
        "password:" {
            send "$passwd\r"
                expect "$user"
                send "sudo su -\r"
                expect "password"
                send "$passwd\r"
                expect "root"
                send "sed -i 's/PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config\r"
                expect "root"
                send "service ssh restart\r"
                expect "root"
                send "passwd\r"
                expect "UNIX"
                send "$rootPasswd\r"
                expect "UNIX"
                send "$rootPasswd\r"
                expect "root"
                send "exit\r"
                expect "$user"
                send "exit\r"
            send_user "服务器 $serverIp 配置修改成功"; exit 0
         }
        timeout { puts "服务器 $serverIp 连接超时"; exit 1 }
    }
    exit 1
    
    • 开始授权authorize.exp
    #!/bin/expect
    set timeout 30
    set serverIp [lindex $argv 0]
    set passwd [lindex $argv 1]
    spawn -noecho ssh-copy-id -i /root/.ssh/id_rsa.pub root@$serverIp
    expect {
                "Are you sure you want to continue connecting (yes/no)?" {
                    send "yes\r"; exp_continue;
                }
                "password:" {
                    send "$passwd\r"; exp_continue;
                }
            eof { exit 0 }
    }
    expect eof
    
    • 确认授权状态confirmServer.sh
    #!/bin/bash
    path=$(cd "$(dirname "$0")"; pwd)
    echo "正在检测所有服务器连接访问状态"
    for i in `awk '{print $1}' ${path}/pwdconfig`
    do
    echo "开始检测服务器${i}的连接状态。。。"
    ssh -o NumberOfPasswordPrompts=0 $i "date" > /dev/null 2>&1
    if [ $? = 0 ]; then
            echo "服务器${i}无密码访问 [\033[32m  成功  \033[0m]"
    else
            echo "服务器${i}无密码访问 [\033[31m  失败  \033[0m]"
    fi
    done
    
    • 配置文件 (ip,用户名,密码,期望修改的root密码) pwdconfig
    192.168.20.109 cloud rd123456 123456
    192.168.20.214 cloud rd123456 123456
    192.168.20.104 cloud rd123456 123456
    192.168.20.100 cloud rd123456 123456
    192.168.20.212 cloud rd123456 123456
    

    针对于我们公司部署情况写的(ubuntu14),如有需求,可适配更改,原理都是一样的。

    相关文章

      网友评论

          本文标题:Linux系统root用户无密码访问脚本(ssh)

          本文链接:https://www.haomeiwen.com/subject/mjifgxtx.html