#!/bin/expect --
########################################## zhichao.hu
#Push the id.pas.pub public key to the target server through the SSH service to implement secret-free login.
#Define an iplist Create an iplist file in the current directory. The content format is: User Password IPaddress Port
#parameter: 1.user
# 2.password
# 3.ip
# 4.Por
##########################################
if {[file isfile /root/.ssh/id_rsa.pub]} {
puts "/root/.ssh/id_rsa.pub file exists"
set ip_file [open ./iplist r]
while {[gets $ip_file line] >= 0} {
set user [lindex $line 0]
set password [lindex $line 1]
set ip [lindex $line 2]
set port [lindex $line 3]
spawn ping ${ip} -w 2
expect {
-nocase -re "100% packet loss" {
send_error "Ping ${ip} is unreachable, Please check the IP address.\n"
exit 1
}
}
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@$ip
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
#already has public key in ~/.ssh/known_hosts
"password:" {
send "$password\r"
}
"Now try logging into the machine" {
#it has authorized, do nothing!
}
}
expect eof
puts "User: $user,\tPassword: xxxx,\tIP: $ip,\tPort: $port\n"
}
close $ip_file
exit 0
} elseif {[file isfile /root/.ssh/id_rsa]} {
puts "/root/.ssh/id_rsa file exists\t\nFile not found in /root/.ssh/id_rsa.pub"
exit 2
} else {
puts "Create an rsa key pair..............................."
} spawn ssh-keygen -t rsa
expect {
"*file in which to save the key*" {
send "\n\r"
send_user "/root/.ssh\r"
exp_continue
"*Overwrite (y/n)*"{
send "n\n\r"
}
}
"*Enter passphrase*" {
send "\n\r"
exp_continue
}
"*Enter same passphrase again*" {
send "\n\r"
exp_continue
}
}
set ip_file [open ./iplist r]
while {[gets $ip_file line] >= 0} {
set user [lindex $line 0]
set password [lindex $line 1]
set ip [lindex $line 2]
set port [lindex $line 3]
spawn ping ${ip} -w 2
expect {
-nocase -re "100% packet loss" {
send_error "Ping ${ip} is unreachable, Please check the IP address.\n"
exit 1
}
}
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@$ip
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
#already has public key in ~/.ssh/known_hosts
"password:" {
send "$password\r"
}
}
puts "User: $user,\tPassword: xxxx,\tIP: $ip,\tPort: $port\n"
}
close $ip_file expect eof

expect 批量自动部署ssh 免密登陆 之 三的更多相关文章

  1. expect 批量自动部署ssh 免密登陆

    [root@node2 ssh]# cat auto_ssh.sh #!/usr/bin/expect -f ########################################## #通 ...

  2. expect 批量自动部署ssh 免密登陆 之 二

    #!/usr/bin/expect -f ########################################## hutu #Push the id.pas.pub public key ...

  3. 批量部署ssh免密登陆

    #!/bin/bash#set -xservers="10.254.192.xx10.254.192.xx10.254.192.xx"passwd="xxxxxxxx&q ...

  4. SSH免密登陆原理及实现

    声明:作者原创,转载注明出处. 作者:帅气陈吃苹果 一.SSH简介 SSH(Secure Shell)是一种通信加密协议,加密算法包括:RSA.DSA等. RSA:非对称加密算法,其安全性基于极其困难 ...

  5. [提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆

    实验说明: 在自动化部署时,会经常SSH别的机器去操作,然而每次的密码认证却很令人烦躁,尤其是很长的密码,因此SSH免密登陆就显得必不可少: 在机器数目很多的时候,使用更过的往往是Ansible分发并 ...

  6. SSH免密登陆配置过程和原理解析

    SSH免密登陆配置过程和原理解析 SSH免密登陆配置过很多次,但是对它的认识只限于配置,对它认证的过程和基本的原理并没有什么认识,最近又看了一下,这里对学习的结果进行记录. 提纲: 1.SSH免密登陆 ...

  7. 使用rsync基于ssh免密登陆进行备份或目录同步

    日常工作中有很多的备份工作,rsync是一个很不错的工具,尝试使用基于ssh免密登陆的方式进行备份,测试成功,是可行且方便的方法,撰文记之,以备后用: 1.A主机root用户对B主机root用户做ss ...

  8. ssh免密登陆:sshpass -p [passwd] ssh -p [port] root@192.168.X.X

    正文: ssh免密登陆:sshpass -p [passwd] ssh -p [port] root@192.168.X.X

  9. ssh免密登陆配置

    目录 ssh免密登陆 在A工作站上输入 B服务器上输入 登陆 ssh初次登陆询问 1.单次取消 2.ansible中增加链接参数 3.修改ansible配置参数[推荐] 4.修改服务器上的ssh_co ...

随机推荐

  1. C: printf参数执行顺序与前置后置自增自减的影响

    起源: 今天在了解副作用side-effect的过程中,看到了下面的网页,把我带到了由printf引起的一系列问题,纠结了一整天,勉强弄懂. 第一个代码没什么好解释的.而第二个printf(" ...

  2. 第一章 初识 MyBatis

    概念:优秀持久层框架:实体类和SQL语句之间建立映射关系 与hibernate区别    :自动生成sql语句,并且建立实体类和数据表的映射. MyBatis基本要素:核心对象   核心配置文件  S ...

  3. Linux(Ubuntu)使用日记------Mongodb的安装与使用

    1.安装 Linux下安装mongodb还是比较容易的 直接使用apt-get安装即可,命令如下: sudo apt-get install mongodb 安装完成之后进行检验, “mongo sh ...

  4. Java代理模式之Cglib代理

    Cglib代理,也叫做子类代理.在内存中构建一个子类对象从而实现对目标对象功能的扩展. CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类.不鼓励直接使用ASM,因 ...

  5. 解决CentOS6.5下MySQL5.6无法远程连接的问题

    在CentOS6.5上安装了Mysql5.6,,本地服务启动成功,但是远程使用Navicat无法远程连接到MySQL数据库,为了解决这个问题,方法如下: (1)先将MySQL服务停掉# service ...

  6. spring cloud配置注册中心显示服务的ip地址和端口

    1.在springcloud中服务的 Instance ID 默认值是: ${spring.cloud.client.hostname}:${spring.application.name}:${sp ...

  7. babel7-按需加载polyfill

    babel7 babel7发布了. 在升级到 Babel 7 时需要注意几个重大变化: 移除对 Node.js 6 之前版本的支持: 使用带有作用域的 @babel 命名空间,以防止与官方 Babel ...

  8. springMVC统一异常处理

    Spring MVC处理异常有3种方式: 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver: 实现Spring的异常处理接口HandlerExc ...

  9. dgraph实现基本操作

    dgraph实现基本操作 简单介绍 dgraph 是一个分布式图数据库 mutate 为一个突变, 一般认为添加数据或者是删除数据为一个突变 query 为一个查询 golang实现dgraph的基本 ...

  10. 洛谷P2604 网络扩容 拆点+费用流

    原题链接 这题貌似比较水吧,最简单的拆点,直接上代码了. #include <bits/stdc++.h> using namespace std; #define N 1000 #def ...