打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux 批量设置无密登陆

* 测试环境: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时每行前缀空格必须清除

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux 远程登录 | 菜鸟教程
第五章 Linux系统的远程登录
使用HeartBeat实现高可用HA的配置过程详解
ssh无密码登陆
用户登陆后,ssh loaclhost无需密码
两台机器之间ssh免密码登录
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服