1.建立一个ip,端口,用户,密码列表

[root@localhost shell-key]# cat arg_list.txt
172.16.56.237 clouds root 172.16.56.215
172.16.56.53 clouds root 172.16.56.215
172.16.56.215 clouds root 172.16.56.215
[root@localhost shell-key]#

2.建立expect交互交脚本:

[root@localhost shell-key]# cat expect_run_config.exp
#!/usr/bin/expect -f
set ip [lindex $argv ]
set passwd [lindex $argv ]
set username [lindex $argv ]
set port [lindex $argv ]
set localip [lindex $argv ]
set ssh_file [lindex $argv ] set remo_ip [lindex $argv ]
set key_name [lindex $argv ]
set timeout #
#create the key in the every node
#生成公共密钥 spawn ssh $username@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$passwd\r" }
} expect "*#\n"
send "rm -fr /root/.ssh\r"
send "mkdir /root/.ssh\r"
send "chmod 700 /root/.ssh\r"
send "cd /root/.ssh\r"
send "ssh-keygen\r" expect "*_rsa):"
send "\r"
expect "*ase):"
send "\r"
expect "*again:"
send "\r" expect "#\n"
send "exit\r"
expect eof spawn ssh $username@$remo_ip expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$passwd\r" }
} expect "*#\n"
send "scp /root/.ssh/id_rsa.pub $username@$localip:/root/shell-key/pub_key/$key_name\r"
expect "*(yes/no)?"
send "yes\r" expect "*password: \n"
send "$passwd\r" #expect "*password:\n"
#send "$passwd\r" expect "*#\n"
send "exit\r"
expect eof [root@localhost shell-key]#

3.建立一个shell循环脚本:

[root@localhost shell-key]# cat loop-out.sh
#!/bin/sh
#loop_sh.sh
echo "########******loding*******#########"
rm -fr /root/shell-key/pub_key/*
rm -fr /root/shell-key/hello.txt while read line
do
echo $line >> hello.txt
done < arg_list.txt
sleep 3 rm -fr /root/shell-key/exp_list.txt while read sl
do
echo $sl | sed "s/$/ ${RANDOM}.key/" >> exp_list.txt
done < hello.txt sleep 3 while read list
do
echo $list | awk '{print $1}'
rem_ip=$(echo $list | awk '{print $1}')
rem_keyname=$(echo $list | awk '{print $6}')
./expect_run_config.exp $list $rem_ip $rem_keyname
done < exp_list.txt cat /root/shell-key/pub_key/* > /root/.ssh/authorized_keys if [ $? -eq 0 ];then
echo "******************************************"
echo "Congratulations, you create a public key *"
echo "******************************************"
else
echo "Please check_your script $0 and expect config! "
echo "Good luck !"
fi
[root@localhost shell-key]#

4.执行脚本:

[root@localhost shell-key]# ./loop-out.sh 

遍地是高手,我是菜鸟中的菜鸟,不喜勿喷!!!

ssh互信自动化脚本(待更新)的更多相关文章

  1. 纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例

    查看完整文章点击原文链接:纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例 你是否还在用postman\jmeter做接口自动化吗?用python的开源框架[unit ...

  2. 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

    20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...

  3. 批量配置SSH互信脚本

    在大规模自动化部署时我们常常需要配置好服务器的SSH互信,以便自动化脚本可以免密登录远程服务器,常规的手动配置SSH互信步骤如下: 使用ssh-keygen生成本地ssh key(mha01),生成的 ...

  4. Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)

    按照集团运维信息安全制度, 需要每个一段时间对线上服务器密码进行一次变更,通过shell脚本部署比较繁琐,所以决定采用ansible脚本对远程主机root密码进行批量重置,该脚本已经在稳定运行在正式环 ...

  5. ezdpl:完全依赖脚本和ssh的自动化部署方案

    ezdpl是easy deployment的简写,使用简单的ssh和shell脚本来部署.升级.回滚和重新配置linux服务器. 重要提示:警告:这个项目还处于测试过程中,请仔细阅读说明,并且自己承担 ...

  6. Linux快速配置集群ssh互信

    之前在<记录一则Linux SSH的互信配置过程>.<Vertica 7.1安装最佳实践(RHEL6.4)>中,都分别提到了配置ssh互信的方法,本文在此基础上进一步整理配置s ...

  7. linux配置ssh互信

    公钥认证的基本思想: 对信息的加密和解密采用不同的key,这对key分别称作private key和public key,其中,public key存放在欲登录的服务器上,而private key为特 ...

  8. Salt自动化之自动更新Gitfs-爱折腾技术网

    Salt自动化之自动更新Gitfs-爱折腾技术网 pygit2

  9. 制作ssh互信的docker镜像

    Dockerfile FROM ubuntu:16.04 # package RUN apt-get update; apt-get -y install ssh COPY ssh_config /e ...

随机推荐

  1. (转)VS自带工具:dumpbin的使用

    有时候我们想查看一个exe引用了哪些动态库,或者我们想看某个动态库包含哪些接口函数,这个时候可以使用dumpbin.exe工具: 1.输入Dumpbin -imports calldll.exe查看它 ...

  2. linux服务端的网络编程

    常见的Linux服务端的开发模型有多进程.多线程和IO复用,即select.poll和epoll三种方式,其中现在广泛使用的IO模型主要epoll,关于该模型的性能相较于select和poll要好不少 ...

  3. jquery之分页插件smartpaginator

    今天推荐一个分页工具条插件:Smart Paginator,这个插件用途还是很广的,而且可定制性相当不错,目前内置三种颜色,有需要的话,可以自己改css定制颜色 1.如何使用Smart Paginat ...

  4. php日期处理

    $datetime=strtotime(date("Y-m-d",time()));//获取当前日期并转换成时间戳 $datetime=$datetime+86400;//在时间戳 ...

  5. c++相关知识回顾

    1.typedef typedef用来定义同类型的同义词.如: typedef unsingned int size_t; typedef int ptrdiff_t; typedef T * ite ...

  6. 利用jquery进行ajax提交表单和附带的数据

    1.获取表单数据: $form.serialize() 2.附带数据:input[status]=1 3.构造url链接:url = $form.attr('action') + '?input[st ...

  7. ubuntu使用github

    Ubuntu下安装Git Ubuntu12.04 LTS默认是已经安装Git的,可以使用 git --version 测试是否安装.如果没有安装,使用命令: sudo apt-get install ...

  8. 使用token机制来验证用户的安全性-b

    登录的业务逻辑{    http:是短连接.         服务器如何判断当前用户是否登录?        // 1. 如果是即时通信类:长连接.    // 如何保证服务器跟客户端保持长连接状态? ...

  9. bzoj 1005: [HNOI2008]明明的烦恼 prufer编号&&生成树计数

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2248  Solved: 898[Submit][Statu ...

  10. ireport常见问题

    $V{PAGE_NUMBER} 表示当前是第几页 ,在text field 的 选项evaluation time选report是共几页,now表是当前页.页码可在ireport里直接设置. &quo ...