#!/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. FLOAT 和 DOUBLE区别

    以下是 FLOAT 和 DOUBLE 的区别: float : 单精度浮点数 double : 双精度浮点数 ·浮点数以 8 位精度存储在 FLOAT 中,并且有四个字节. ·浮点数存储在 DOUBL ...

  2. mysql 8.0.X 创建新的数据库、用户并授权

    一.创建数据库 mysql> create database jira; Query OK, 0 rows affected (0.09 sec) 二.创建用户 mysql> create ...

  3. jmeter学习记录--10--二次开发环境搭建

    JMeter源码集成到Eclipse.JMeter二次开发(1)-eclipse环境配置及源码编译 ,根据此文章记录将jmeter源码集成到myecplise 第一步:下载jmeter源码http:/ ...

  4. 3 Eclipse 查看不了源码

    Eclipse 查看源码时,报source not found问题,经查资料,需要配置环境变量才能查看到. 在用户变量或者系统变量下,配置CLASSPATH,值为JDK的lib路径:D:\Progra ...

  5. [转帖] Linux 时间参数

    Linux下文件的三个时间参数 https://blog.csdn.net/GGxiaobai/article/details/53609478 想用touch修改创建时间呢 发现不知道怎么修改来着. ...

  6. centos关机与重启命令

    Linux centos重启命令: 1.reboot   普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户 ...

  7. 剑指Offer面试题 二维数组中的查找

    题目链接:https://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e 题目大意: 略 分析: 对萌醒很开拓思维 ...

  8. 小项目一---Python日志分析

    日志分析 概述 分析的前提 半结构化数据 文本分析  提取数据(信息提取)  一.空格分隔 with open('xxx.log')as f: for line in f: for field in ...

  9. navicat导入sql文件错误

    场景:100多M的sql文件导入到本地数据库报错,本地环境,phpstudy,报错一1294 - Invalid ON UPDATE clause for 'create_time' column,报 ...

  10. [HNOI2007]神奇游乐园(插头DP)

    题意:n*m的矩阵内值有正有负,找一个四连通的简单环(长度>=4),使得环上值的和最大. 题解:看到2<=m<=6和简单环,很容易想到插头DP,设f[i][j][k]表示轮廓线为第i ...