美文网首页
linux 批量设置无密登陆

linux 批量设置无密登陆

作者: codingparty | 来源:发表于2015-12-24 10:44 被阅读289次

* 测试环境:Red Hat Enterprise Linux Server release 6.6 (Santiago)


1、创建密钥

        [root@localhost ~]# ssh-keygen
        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/alei/.ssh/id_rsa):
        Created directory '/home/alei/.ssh'.
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /home/alei/.ssh/id_rsa.
        Your public key has been saved in /home/alei/.ssh/id_rsa.pub.
        The key fingerprint is:
        e2:82:86:66:71:58:f1:2c:68:a0:aa:b6:ea:a2:02:a5 alei@localhost.localdomain
        The key's randomart image is:
        +--[ RSA 2048]----+
        |.  .            |
        |o . +            |
        |.o o o          |
        |o + .            |
        |.= .  . S        |
        |E.o. . .        |
        |++o . .          |
        |*..  .          |
        |Oo              |
        +-----------------+

2、安装Expect工具

         A:搜索expect工具

              [alei@localhost ~]$ yum search expect
              ....
              pexpect.noarch : Pure Python Expect-like module
              expect.x86_64 : A program-script interaction and testing utility
              expect-devel.i686 : A program-script interaction and testing utility
              expect-devel.x86_64 : A program-script interaction and testing utility
              expectk.x86_64 : A program-script interaction and testing utility
              ....

        B:安装expect工具
              [alei@localhost ~]# yum install  expect.x86_64  -y

              ...
              Resolving Dependencies
              --> Running transaction check
              ---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
              --> Finished Dependency Resolution
              ...
              Running transaction
                Installing : expect-5.45-14.el7_1.x86_64                                  1/1
                Verifying  : expect-5.45-14.el7_1.x86_64                                  1/1
              Installed:
                expect.x86_64 0:5.45-14.el7_1                                        
              Complete!

3、开发交互脚本

              [alei@localhost ~]# vim run.exp

              #!/usr/bin/expect

              set username [lindex $argv 0]
              set userpwd [lindex $argv 1]
              set serverip [lindex $argv 2]
              set timeout 30
              spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub $username@$serverip
              expect {
                      timeout exit
                      yes/no  {send "yes\r";exp_continue}
                      password {send "$userpwd\r"}
              }
              interact

4、开发执行脚本

              [alei@localhost ~]# vim run.sh
              #!/bin/sh
              if [ -z $* ];then
                      echo "Usage: $0 <ipfile>"
                      exit
              fi
              ipspath="$1"
              username="alei"
              userpwd="alei"
              successfile="./success"
              failedfile="./failed"
              rm -rf $successfile
              rm -rf $failedfile
              for ip in $(cat $ipspath)
              do
                      echo "<<<<<<<<<<<<<<<<<<<<<<<<<$ip<<<<<<<<<<<<<<<<<<<<"
                      ./run.exp $username $userpwd $ip
                      if [ $? -eq 0 ];then
                              echo $ip >> $successfile
                      else
                              echo $ip >> $failedfile
                      fi
              done

5、准备要设置无密登录的服务器

              [alei@localhost ~]# vim ips_list
              192.168.0.10
              192.168.0.11
              192.168.0.12
              192.168.0.13
              192.168.0.14
              192.168.0.15
              192.168.0.16

6、开始设置无密登录

              [alei@localhost ~]$ sh run.sh ips_list

              <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.10<<<<<<<<<<<<<<<<<<<<

              spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub alei@192.168.0.10

              alei@192.168.0.10's password:

              Now try logging into the machine, with "ssh 'alei@192.168.0.10'", and check in:

             .ssh/authorized_keys

              to make sure we haven't added extra keys that you weren't expecting.

              <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.11<<<<<<<<<<<<<<<<<<<<

             spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub alei@192.168.0.11

             alei@192.168.0.11's password:

             Now try logging into the machine, with "ssh 'alei@192.168.0.11'", and check in:

             .ssh/authorized_keys

             to make sure we haven't added extra keys that you weren't expecting.

7、注意事项

          A:如果执行脚本时发生以下错误,检查run.exp文件的执行权限(alei@localhost ~]# ll run.exp ),若没有需要添加([alei@localhost ~]# chmod +x  run.exp)

                <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.10<<<<<<<<<<<<<<<<<<<<

                run.sh: line 16: ./run.exp: Permission denied

           B:如果使用以上脚本信息,copy时每行前缀空格必须清除

相关文章

  • linux 批量设置无密登陆

    * 测试环境:Red Hat Enterprise Linux Server release 6.6 (Santi...

  • Linux无密码登陆及命令应答交互

    一、Linux无密码登陆 a.通过设置KEY登陆(推荐)生成密钥:ssh-keygen 安...

  • linux 使用163邮箱给运维人员发送邮件

    linux 发送邮件 Linux 使用163邮箱发送邮件原理 163开启第三方登陆并授权 设置这个页面会有个授权密...

  • Linux 设置免密登陆(Mac)

    设置ssh 免密登陆 前置条件: 安装ssh-copy-idbrew install ssh-copy-id 登陆...

  • git服务器

    首先连接自己的云服务器 添加git 用户,SSH 无密登陆 *git用户属于Linux,对应于root用户,不创建...

  • 免密登陆设置

    问题参考博客:https://blog.csdn.net/zhangmingcai/article/details...

  • Linux免密登陆

    参考 Linux机器之间免密登录设置 1. 配置两台Linux服务器,均为Centos7 master: 192....

  • linux 免密登陆

    1. 在开始之前,你需要了解一些情况 用 SSH 公钥认证从而连接登陆远程服务器是一个强大的功能,比通过账号密码登...

  • Linux 免密登陆

    步骤一: 生成本地密匙 如果本地没有密匙, 首先需要创建一个自己的密匙,使用ssh-keygen -t rsa,一...

  • Mac与Linux SSH无密登陆(互信)

    导语 最近接到任务要帮队友搭建spark集群,其中涉及到hadoop集群,然后需要ssh互信,我其实还是第一次接触...

网友评论

      本文标题:linux 批量设置无密登陆

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