Python模块实战使用openpyxl 获取将chage -l user检查结果文件进行2次加工检查

版权声明:本文为博主原创文章,未经博主允许不得转载。原创不易,各位勉之。 https://blog.csdn.net/LL845876425/article/details/86516033

chage 命令是一个检查Linux系统用户有效期的一个工具,当集群主机过多时,部分自研的运维工具都可以批量执行命令导出excel。
脚本如下:

#########################################################
# Function: 使用openpyxl 获取将chage -l user检查结果文件进行2次加工检查
# Author : haiyuan
# Description:
#
#=======================================================
# Date          ID(Name)        Version         Status
# 2019/01/16    haiyuan        V1.0            create
#########################################################

import openpyxl

file_path =r'D:\python_learn_script\openpyxl\chage_root_reslut.xlsx'
wb = openpyxl.load_workbook(filename=file_path)

sheet = wb.active

# [root@neutron ~]# chage -l root
# Last password change                                    : Nov 14, 2018
# Password expires                                        : never
# Password inactive                                       : never
# Account expires                                         : never
# Minimum number of days between password change          : 0
# Maximum number of days between password change          : 99999
# Number of days of warning before password expires       : 7

if sheet == None:
    print('get workbook failed.\nsheet is none.')
    exit(2)

sheet['D1'] = 'Last password change'
sheet['E1'] = 'Password expires'
sheet['F1'] = 'Password inactive'
sheet['G1'] = 'Account expires'

# check column in workbook
check_column = 3

for row in range(1, sheet.max_row + 1):
    chage_value = str(sheet.cell(row=row, column=check_column).value)
    chage_value_detail = chage_value.split('\n')
    # print(chage_value)
    # print(chage_value_detail)
    for host_info in chage_value_detail:
        print(host_info)
        if sheet['D1'].value in host_info:
            cell = 'D' + str(row)
            sheet[cell] = host_info.split(': ')[1]
            # print(sheet[cell].value)
            if sheet[cell].value == 'never':
                continue
            else:
                print('cell %s value is not never.real value is %s.\n' % (cell, sheet[cell].value))
        elif sheet['E1'].value in host_info:
            cell = 'E' + str(row)
            sheet[cell] = host_info.split(': ')[1]
            # print(sheet[cell].value)
            if sheet[cell].value == 'never':
                continue
            else:
                print('cell %s value is not never.real value is %s.\n' % (cell, sheet[cell].value))
        elif sheet['F1'].value in host_info:
            cell = 'F' + str(row)
            sheet[cell] = host_info.split(': ')[1]
            # print(sheet[cell].value)
            if sheet[cell].value == 'never':
                continue
            else:
                print('cell %s value is not never.real value is %s.\n' % (cell, sheet[cell].value))
        elif sheet['G1'].value in host_info:
            cell = 'G' + str(row)
            sheet[cell] = host_info.split(': ')[1]
            # print(sheet[cell].value)
            if sheet[cell].value == 'never':
                continue
            else:
                print('cell %s value is not never.real value is %s.\n' % (cell, sheet[cell].value))
        else:
            # print('error,please check.')
            continue

wb.save(file_path)

执行前文件截图:

执行脚本后文件截图:

猜你喜欢

转载自blog.csdn.net/LL845876425/article/details/86516033