您的位置:首页 > 其它

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

2018-06-03 13:42 411 查看

    为了维护方便,生产环境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"
#授权信息归集文件
auth_message="message.csv"

#在各节点生成"主机名_用户"格式的授权文件
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 $auth_message ]];then
    echo > $auth_message
fi
for i in `ls $authfile_dir`
    do
        host_user=`echo $i|awk -F "." '{print $1}'`
        message=`cat $authfile_dir/$i|awk '{print $3}'|sort -u|awk '{{printf"%s,",$0}}'`
        printf "%s,%s\n" $host_user $message >> $auth_message
    done
}
cpauthfile
create_table

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




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ansible 免密 登录