码迷,mamicode.com
首页 > 其他好文 > 详细

统计各节点ssh免密登录授权信息

时间:2018-06-03 14:26:03      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:ansible

    为了维护方便,生产环境ansible到其他所有节点所有账号做了免密登录,根据安全需求,现需要统计所有节点所有账号的ssh免密登录授权信息。为了省时省力,将以ansible+shell的形式实现,如下:

1、准备好ansible的hosts文件,根据账号名称分成多个组,其中root组包括所有节点的IP地址。

2、准备一个文件包括所有节点的IP地址

3、playbook文件,authfile.yml

---
- hosts: root
  gather_facts: false
  remote_user: root
  tasks:
    - name: cp root auth file
      shell: source ~/.bash_profile && source /etc/profile && mkdir -p /tmp/myauthdir && cp /root/.ssh/authorized_keys /tmp/myauthdir/`hostname`_root.txt
- hosts: sumapay
  gather_facts: false
  remote_user: root
  tasks:
    - name: cp sumapay auth file
      shell: source ~/.bash_profile && source /etc/profile && cp /home/sumapay/.ssh/authorized_keys /tmp/myauthdir/`hostname`_sumapay.txt
- hosts: sumapay25
  gather_facts: false
  remote_user: root
  tasks:
    - name: cp sumapay25 auth file
      shell: source ~/.bash_profile && source /etc/profile && cp /home/sumapay25/.ssh/authorized_keys /tmp/myauthdir/`hostname`_sumapay25.txt
- hosts: glassfish
  gather_facts: false
  remote_user: root
  tasks:
    - name: cp glassfish auth file
      shell: source ~/.bash_profile && source /etc/profile && cp /home/glassfish/.ssh/authorized_keys /tmp/myauthdir/`hostname`_glassfish.txt

4、shell脚本文件,authfile.sh

#!/bin/bash
#授权文件归集目录
authfile_dir="authfile"

#在各节点生成"主机名_用户"格式的授权文件
ansible-playbook -i /etc/ansible/hosts authfile.yml 
#拷贝各节点生成后的授权文件
cpauthfile(){
if [[ ! -d $authfile_dir ]];then
    mkdir $authfile_dir
fi
for i in `cat ip.txt`
    do
        scp root@${i}:/tmp/myauthdir/* $authfile_dir
    done
}
#过滤个授权文件免密登录主机,并生成列表
create_table(){
if [[ -s filename.txt ]];then
    echo > filename.txt
fi
for i in `ls $authfile_dir`
    do
        echo $i|awk -F "." '{print $1}' >> filename.txt
        cat $authfile_dir/$i|awk '{print $3}'|sort -u >> filename.txt
        echo "" >> filename.txt
    done
}
cpauthfile
create_table

5、执行authfile.sh,生成filename.txt文件,包含所有节点、所有用户的ssh免密登录授权信息。




统计各节点ssh免密登录授权信息

标签:ansible

原文地址:http://blog.51cto.com/fengjicheng/2123625

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!