知识点033-利用expect和shell分发密钥以后用ansible统计哪些没有分发成功

环境准备:

linux 操做系统python

expect分发密钥linux

sublime绿色版本shell

第一阶段:分发密钥

选择一个目录里面新建一个.exp文件,里面内容为:windows

#!/usr/bin/expect

if { $argc !=2 } {

send_user "usege:expect 320.exp file host"  

exit

}

#define var

set file [lindex $argv 0] 

set host [lindex $argv 1]

set password "123456"  

spawn ssh-copy-id -i $file "s-linuxad@$host"  

expect {  

"yes/no" {send "yes\r";exp_continue}  

"*password" {send "$password\r"}

}

expect eof

而后新建一个shell 脚本:api

#!/bin/bash

. /etc/init.d/functions

for ip in `cat list` 

do

expect 320.exp ~/.ssh/id_dsa.pub $ip  

if [ $? -eq 0 ];then  

action "$ip" /bin/true  

else

action "$ip" /bin/false

fi

done

新建一个list 文件,里面放入你要分发的密钥便可bash

[s-linuxad@T-Ansible-v-szzb scripts]$ cat list
172.31.1.32
172.31.1.221
10.0.5.146
10.0.100.10 
10.0.100.11

执行的时候能够选择把输出过程输出到一个文件里面:./start.sh >/tmp/file.txt 能够追溯回去服务器

第二阶段  测试哪些密钥没有分发成功

利用ansible 的ping 模块来操做 主要编辑/etc/ansible/hosts的文件,通常150台服务器放一个模块ssh

利用命令  ansible hosts -m ping >/tmp/file.xt测试

查看输出文件的内容网站

10.0.100.19 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 
    "unreachable": true
}
10.0.5.146 | SUCCESS => {
    "changed": false, 
    "ping": "pong"

文件中success则为分发密钥成功的,若是出现不是success 就是没有成功的

第三阶段   利用本地sublime 和python 选取没有分发成功的数据

去网站下sublime 和python  windows版本

https://www.python.org/

http://www.sublimetext.com/3

 

注:安装时勾选“Add to explorer context menu”,表示添加到鼠标的右键菜单 

安装插件管理器 Package Control

 

使用 [Ctrl + `] (或View > Show Console menu) 打开Sublime Text控制台,将下面的Python代码粘贴到控制台后按回车。

import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

用Package Control安装插件

按Ctrl+Shift+P调出命令面板
输入Install Package 选项并回车。
输入要安装的插件名,回车安装

安装 Anaconda 插件

利用Package Control安装Anaconda。安装后修改Anaconda配置文件:

Preferences > Package Settings > Anaconda > Settings – User

系统属性-环境变量

{  "pep8_ignore":
    [
        "E501",
    ],
    "complete_parameters": true,
    "anaconda_linting_behaviour": "save-only",// always
    "anaconda_gutter_theme": "hard",
    "anaconda_linter_show_errors_on_save": true,
    "python_interpreter": "C:/Python35-32/python.exe",
    "anaconda_linting": false,
}

其中"python_interpreter"改成本身电脑的python的路径。保存后重启sublime

添加脚本.py,注意本地要保存的扩展名是.py的

import re 
import os 

result_path = 'D:\\tmp\\'

os.chdir('D:\\tmp\\ansible-ping')
ip_files = os.listdir(os.getcwd())
print(ip_files)b

for ip_file in ip_files:
	with open(ip_file) as f:
		c = f.read()
		ip_list = re.findall(r'([\d\.]+).*[^SUCCESS] =>', c)

	with open(os.path.join(result_path, ip_file), 'w') as f:
		ip_list = map(lambda x: x+os.linesep, ip_list)
		f.writelines(ip_list)

最后执行的结果:

相关文章
相关标签/搜索