Expect安装

[root@web02 scripts]# yum install expect

SSH密钥生成

[root@web02 scripts]# ssh-keygen -t dsa   <==生成密钥

Generating public/private dsa key pair.

Enter file in which to save the key (/root/.ssh/id_dsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_dsa.

Your public key has been saved in /root/.ssh/id_dsa.pub.

The key fingerprint is:

d8:a6:e4:1f:8a:9a:25:f9:39:6c:74:1e:69:78:78:11 root@web02

The key's randomart image is:

+--[ DSA 1024]----+

|                 |

|      E          |

|       .         |

|      .o         |

|     oooS        |

|   .+oBo         |

|  oo.*o..        |

|   =+o.o .       |

|  oo+.. .        |

+-----------------+

 

密钥分发

[root@web02 scripts]# vi ssh_handout.expect  <==脚本编写

#!/usr/bin/expect -f

set ip [lindex $argv 0 ]    <==接受第一个参数,赋值ip

set password 118530  <==被分发主机密码

set timeout 10

cd ~

spawn ssh-copy-id -i .ssh/id_dsa.pub root@$ip  <==发送  .ssh/authorized_keys到目标主机

expect {

"*yes/no" { send "yes\r"; exp_continue}      <==遇见“YES/NO”  发送命令“YES”

"*password:" { send "$password\r" }          <==遇见“password” 发送命令“密码”

}

expect "#*"

#send "pwd\r"

#send  "exit\r"

expect eof

~

~

~

~

~

~

~

~

~

"ssh_handout.expect" 15L, 305C written

[root@web02 scripts]# ./ssh_handout.expect 192.168.162.130  <==测试

spawn ssh-copy-id -i .ssh/id_dsa.pub root@192.168.162.130

root@192.168.162.130's password:

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

.ssh/authorized_keys   <==表示正确

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

expect: spawn id exp4 not open

while executing

"expect eof "

(file "./ssh_handout.expect" line 14)

[root@web02 ~]# ssh root@192.168.162.130      <==免密码登陆

Last login: Thu Jul 21 19:59:12 2016 from 192.168.162.131

[root@web01 ~]# logout

Connection to 192.168.162.130 closed.

[root@web02 ~]# scp data/a.txt root@192.168.162.130:/tmp/  <==免密码传送文件

a.txt                  100%   13     0.0KB/s   00:00

[root@web02 ~]# ssh root@192.168.162.130

Last login: Thu Jul 21 20:01:20 2016 from 192.168.162.131

[root@web01 ~]# ls /tmp/

a.txt  yum.log

[root@web01 ~]# logout

Connection to 192.168.162.130 closed.

[root@web02 ~]#

[root@web02 scripts]# vi handout.sh   <==分发到多个主机脚本

#aim:handout ssh to host

#!/bin/bash

#aim:handout ssh to host

#author:changyaoguo

#date:2016/07/21

. /etc/init.d/functions

for n in 128 129 130  132 133   <==每个主机的部分主机号

do

/server/scripts/ssh_handout.expect 192.168.162.$n >/dev/null 2>&1

action "ssh  192.168.162.$n" /bin/true

done

~

~

~

~

"handout.sh" [New] 10L, 245C written

[root@web02 scripts]# sh handout.sh       <==测试

ssh  192.168.162.128                                       [  OK  ]

ssh  192.168.162.129                                       [  OK  ]

ssh  192.168.162.130                                       [  OK  ]

ssh  192.168.162.132                                       [  OK  ]

ssh  192.168.162.133                                       [  OK  ]

希望点个赞!!!!

ssh+expect批量分发的更多相关文章

  1. ssh密钥批量分发

    #################以下所有的命令脚本都是在centos7系统上实现,centos6略有不同 客户端使用公钥连接服务器的步骤: 提起ssh我们就能想到的是远程连接,平时我们都是通过密码来 ...

  2. expect批量分发公钥

    sshkey.exp #!/usr/bin/expect# 由于是多台服务器,需要在shell脚本中循环调用该脚本 if { $argc != 2 } { send_user "usage: ...

  3. expect批量分发密钥对

    vim shell.exp #!/usr/bin/expect set timeout 10 set hostname [lindex $argv 0] set username [lindex $a ...

  4. SSH KEY 批量分发

    代码 #!/bin/sh . /etc/init.d/functions ];then echo "sh $0 arg0" exit fi for ip in 172.23.216 ...

  5. 【SSH项目实战三】脚本密钥的批量分发与执行

    [SSH项目实战]脚本密钥的批量分发与执行 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载, ...

  6. 【SSH项目实战】脚本密钥的批量分发与执行【转】

    [TOC] 前言 <项目实战>系列为<linux实战教学笔记>第二阶段内容的同步教学配套实战练习,每个项目循序衔接最终将组成<Linux实战教学笔记>第二阶段核心教 ...

  7. ssh密钥分发之二:使用sshpass配合ssh-kopy-id编写脚本批量分发密钥:

    使用sshpass配合ssh-kopy-id编写脚本批量分发密钥: 首先sshpass是一个ssh连接时的免交互工具,首先要安装一下: yum install sshpass -y 接下来我们就可以使 ...

  8. (转)SSH批量分发管理&非交互式expect

    目录 1 SSH批量分发管理 1.1 测试环境 1.2 批量管理步骤 1.3 批量分发管理实例 1.3.1 利用sudo提权来实现没有权限的用户拷贝 1.3.2 利用sudo提权开发管理脚本 1.3. ...

  9. (转)Linux SSH批量分发管理

    Linux SSH批量分发管理 原文:http://blog.51cto.com/chenfage/1831166 第1章 SSH服务基础介绍 1.1 SSH服务 1.1.1SSH介绍 SSH是Sec ...

随机推荐

  1. ZeroMQ接口函数之 :zmq_ctx_destroy - 销毁一个ZMQ环境上下文

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_destroy zmq_ctx_destroy(3) ØMQ Manual - ØMQ/3.2.5 Nam ...

  2. 用maven配置springboot+freemarker

    1.创建项目 直接点下一步   原因: 不勾选 Create from archetype,是项目创建的骨架的时候,由于不知道什么原因就卡住了,一直在刷新 2.创建之后完成之后 添加依赖 <pa ...

  3. 随鼠标轮动翻动层————jquery小练习

    闲来无事在网站上看见一个网页制作的不错,就仿照做来看看.特此记录下来. 亮点:随鼠标上下滚动,展示页面随之不同,翻动效果. 功能点:鼠标向上,向下判断事件. css 代码 html { overflo ...

  4. js-倒计时自动隐藏

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  5. c++字符串

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  6. STL学习之vector

    vector是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩展.它可以像指针一样被操作,由于它的特性我们完全可以将vector看做动态数组. 特点: 1.指定一块如同数组一样的连续存 ...

  7. JQUERY attr prop 的区别 一个已经被淘汰

    在做jquery 全选 全不选的项目中, 1..prop( propertyName ) 获取匹配集合中第一个元素的Property的值 2. .prop( propertyName, value ) ...

  8. 国家以及国家语言的json数据格式,提供给网友参考。

    目前接触到一个需求,需要全球主要国家的选择以及语言的选择,如下图: 这是国家选项 这是语言的选项: 很简单有木有? 本来打算直接给文件,但是好像没有文件上传,所以就提供一个思路和代码,你们照着弄吧. ...

  9. svn中第一次check out working copy项目下来出现 ld: library not found for -lcrypto clang: error: linker command failed with exit code 1 (use -v to see invocation)

    这个问题主要是.a文件的忽略删除,需要更改设置,并且把文件重新添加

  10. 【转】C#中将JSon数据转换成实体类,将实体类转换成Json

    http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...