ansible批量分发免密钥登陆python脚本
最近看了看强大的号称自动化运维的三大利器之一的——ansible,ok,亲测之后,确实感觉,对于我们这种DBA工作者来说,确实很受益。
值得注意的是ansible要求被管理服务器python版本不低于2.6。
OK,简单了写了个脚本,实现服务器端面密钥登陆被管理服务器python脚本,这里省略了ansible,python等的部署以及使用。
环境:
python 2.7
ansible 2.4.0
pexpect (pip installpexpect)
以下是批量分发密钥登陆脚本内容:
#!/usr/bin/python27
""" Made by Joe.Wan Email:1272149624@QQ.com"""
import sys
import pexpect ip = ['10.1.1.10','10.1.1.13'] #这是ip列表,视情况修改 for x in ip:
password='asss' #密码需要视情况修改
expect_list = ['(yes/no)','password:'] p = pexpect.spawn('ssh-copy-id %s' % x) try:
while True:
idx = p.expect(expect_list)
print p.before + expect_list[idx],
if idx == :
print "yes"
p.sendline('yes')
elif idx == :
print password
p.sendline(password)
except pexpect.TIMEOUT:
print >>sys.stderr,'timeout'
except pexpect.EOF:
print p.before
print >>sys.stderr,'<the end>'
执行结果:
ok,将上面脚本修改下,弄成单个分发脚本:addhost.py
#!/usr/bin/python import sys
import pexpect ip = sys.argv[1] password = 'password'
expect_list = ['(yes/no)', 'password:'] p = pexpect.spawn('ssh-copy-id %s' % ip)
try:
while True:
idx = p.expect(expect_list)
print p.before + expect_list[idx],
if idx == 0:
print "yes"
p.sendline('yes')
elif idx == 1:
print password
p.sendline(password)
except pexpect.TIMEOUT:
print >>sys.stderr, 'timeout'
except pexpect.EOF:
print p.before
print >>sys.stderr, '<the end>'
使用方法:python27 addhost.py 10.1.1.1
ansible批量分发免密钥登陆python脚本的更多相关文章
- 使用ansible 批量分发ssh密钥
先看执行结果 看看ssh.yml怎么写的 看看hosts文件中backup组怎么写的
- ssh免密钥登陆的两种方式
ssh 免密钥登陆的两种方式第一种:直接使用命令复制过去ssh-copy-id root@192.168.3.113批量复制for i in {113..140}; do ssh-copy-id ro ...
- 批量实现SSH无密码登陆认证脚本
批量实现SSH无密码登陆认证脚本 问题背景 使用为了让linux之间使用ssh不需要密码,可以采用了数字签名RSA或者DSA来完成.主要使用ssh-key-gen实现. 1.通过 ssh-key-ge ...
- 批量SSH key-gen无密码登陆认证脚本 附件脚本
# 批量实现SSH无密码登陆认证脚本 ## 问题背景 使用为了让linux之间使用ssh不需要密码,可以采用了数字签名RSA或者DSA来完成.主要使用ssh-key-gen实现. 1.通过 ssh-k ...
- SSH免密钥登陆
local ipaddress:10.47.39.7:remote ipaddress:10.47.39.8 1.生成公钥和私钥 [root@local ~]# ssh-keygen -t rsa ...
- centos7.5下生成公钥,实现ssh免密钥登陆
配置SSH无密码登录需要4步准备工作生成公钥和私钥导入公钥到认证文件,更改权限测试1. 准备工作确认本机sshd的配置文件(需要root权限) # vi /etc/ssh/sshd_config 1找 ...
- shell 练习 (免密钥登陆脚本)
脚本说明 本地服务器ip 10.0.0.5 远程服务器地址 10.0.0.223 #!/bin/bashremote_ip=$ if [ ! -n "$1" ] ;then ech ...
- 快速做ssh免密钥登陆
1.本地生成密钥并发送目标主机 >&1 #采用数字签名RSA或DSA生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub ssh-copy-id ...
- 批量SSH key-gen无密码登陆认证脚本
SSH key-gen无密码登录认证脚本 使用为了让linux之间使用ssh不需要密码,可以采用了数字签名RSA或者DSA来完成.主要使用ssh-key-gen实现. 通过 ssh-key-gen 来 ...
随机推荐
- [leetcode-523-Continuous Subarray Sum]
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- 7.java的请求转发和请求重定向
1.请求重定向:是客户端的行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保存,地址栏的URL地址会改变,一次新的转发. 2.请求转发:是服务器的行 ...
- 容器如何访问外部世界?- 每天5分钟玩转 Docker 容器技术(36)
前面我们已经解决了容器间通信的问题,接下来讨论容器如何与外部世界通信.这里涉及两个方向: 容器访问外部世界 外部世界访问容器 容器访问外部世界 在我们当前的实验环境下,docker host 是可以访 ...
- SDP
SDP语法 一个SDP描述含有会话级信息和媒体级信息.会话级信息应用于整个会话.例如:它能成为会话始发者或者会话的名字.媒体级信息作用于特殊的媒体流.例如:它能作为一个编码器给音频流编码或者是给视频流 ...
- AT&T汇编helloworld
摘自:http://blog.163.com/guixl_001/blog/static/417641042012112102642703/ 代码: #hello.s .data # 数据段声明 ms ...
- ActiveMQ 503错误
问题描述: 在Linux系统下安装ActiveMQ,启动服务 正常启动后,通过浏览器进行访问 可以正常显示home页面,但是点击其他菜单,如Queues,Topics等,都会出现503错误,如图 问题 ...
- 读书共享 Primer Plus C-part 8
第十三章 文件输入/输出 fopen--fclose fopen 是文件的操作的开始 fclose是文件操作的结束 getc--putc getchar--putchar getc对应文件的获取单个字 ...
- centos7使用cobbler(2.8)批量部署操作系统之二
1. Cobbler常用命令 1.1 查看cobbler帮助 # cobbler --help usage ===== cobbler <distro|profile|system|repo|i ...
- Kubernetes服务之“运行单实例的有状态服务”
目标 在你的环境中创建一个PV 创建一个MySQl的Deployment 在集群中以DNS名称的方式,将MySQL暴露给其他的pod 开始之前 你需要一个Kubernetes集群,一个可以连接到集群的 ...
- 初步研究一下sourceTree
今天研究sourceTree,在此小结一下 1.下载链接:https://www.atlassian.com/software/sourcetree 2.安装,注册账户登录,连接到GitHub账号上, ...