#!/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. Web项目中的 /

    如果 /  出现在路径的前面: web.xml中:http://loclalhost:8080/项目名称/      在项目的根路径下面 jsp中: http://localhost:8080/    ...

  2. Java队列学习

    队列是Java集合中的重要组成部分,具有先进先出的特性,使其具有广泛的应用场景,比如排队等.因此今天就来学习一下Java中的队列.本文的例子使用的Java8环境. 继承类图 学习队列,首先要知道它的类 ...

  3. 在windows环境利用celery实现简单的任务队列

    测试使用环境: 1.Python==3.6.1 2.MongoDB==3.6.2 3.celery==4.1.1 4.eventlet==0.23.0 Celery分为3个部分 (1)worker部分 ...

  4. java订单金额分级计算

    package ord; import java.util.ArrayList; public class order { public String orderid; public user use ...

  5. feign多文件上传

    参考地址:https://www.cnblogs.com/standup/p/9090113.html https://www.cnblogs.com/standup/p/9093753.html 1 ...

  6. 拒绝回调,拥抱async await

    之前使用jquery中ajax,请求的结果需要写在回调函数里面,后面接触到了axios/fetch,使用了es6中Promise进行封装,这种链式结构调用,看起来比之前直观,可是还是没有解决回调的问题 ...

  7. 基于Redis实现分布式锁

    分布式锁具有的特性: 1.排他性: 文件系统: 数据库:主键 唯一约束 for update 性能较差,容易出现单点故障 锁没有失效时间,容易死锁 缓存Redis:setnx 实现复杂: 存在死锁(或 ...

  8. Oracle数据库启动出现ORA-27101错误之ORA-19815处理方式及数据备份

    ORA-27101: sharedmemory realm does not exist之ORA-19815处理 重启数据库(数据库:muphy),登陆是越到错误: ORA-27101: shared ...

  9. 七.django模型系统(一)

    Ⅰ.django的ORM 1.含义 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语 ...

  10. rocketmq 启动和停止命令

    A启动server: nohup sh mqnamesrv &A启动broken: nohup sh mqbroker -c /opt/rocketmq432/conf/2m-noslave/ ...