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. 后台动态生成GridView列和模版

    考虑到很多数据源是不确定的,所以这时无法在前台设置gridview的表头,需要在后台动态指定并绑定数据. 前台代码如下: <%@ Page Title="主页" Langua ...

  2. 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用

    一.前言:  A.Arduino简介 Arduino是由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Banzi.David Cuartielles.Tom Igoe.Gianluc ...

  3. Python for Infomatics 第12章 网络编程五(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.8 用urllib读取二进 ...

  4. std::string的split函数

    刚刚要找个按空格分离std::string的函数, 结果发现了stackoverflow上的这个问题. 也没仔细看, 直接拿来一试, 靠, 不对啊, 怎么分离后多出个空字符串, 也就是 "a ...

  5. for循环

    1.使用for循环输出矩形 public static void print1(int h,int w){        for(int i=0; i<h; i++){            f ...

  6. HDU1426 DFS

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. Linux中syntax error near unexpected token 错误提示解决方法

    Linux中syntax error near unexpected token ... 错误提示有一般有两种原因: 1)window和Linux下换行符不一致导致 window下的换行和Linux下 ...

  8. USACO翻译:USACO 2014 US Open 三题

    USACO 2014 US Open 一.题目概览 中文题目名称 牧场装饰 里程表 牛像展览 英文题目名称 decorate odometer fairphoto 可执行文件名 decorate od ...

  9. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  10. php多线程详解

    在说明多线程的题前,需要弄清楚以下几个问题 1,ts 和 nts的区别 Thread Safe和NoneThread Safe 先说windows的,在php官网,在windows区域有在文件下在有 ...