Python 从tac_plus文件获取用户和组

Python 从tac_plus文件获取用户和组,python,linux,Python,Linux,我的任务是从Linux环境中的tac_plus文件中获取用户和组。 我之前的那个人创建了一个不再工作的python脚本,我自己也不是python人 该脚本主要用于输出用户的名字和姓氏以及访问组 下面是该人员创建的脚本。如果有人能透露一些信息,那将是非常感激的 #!/usr/bin/python # v0.1 read_tacacs.py # Read tac_plus.cfg file and grep username/full_name/access type into column li

我的任务是从Linux环境中的tac_plus文件中获取用户和组。 我之前的那个人创建了一个不再工作的python脚本,我自己也不是python人

该脚本主要用于输出用户的名字和姓氏以及访问组


下面是该人员创建的脚本。如果有人能透露一些信息,那将是非常感激的

#!/usr/bin/python
# v0.1 read_tacacs.py
# Read tac_plus.cfg file and grep username/full_name/access type into column list.

from datetime import datetime

i = datetime.now()

user_name = []
full_name = []
group_access = []


with open('/usr/local/etc/tac_plus.cfg', 'r') as searchfile:
        for line in searchfile:
                if 'user =' in line:
                        user_name.append(line.split()[2])
                if 'Full Name:' in line:
                        full_name.append(line.split()[3]+ ' ' + line.split()[4])
                if 'member' in line:
                        if '@all' in line:
                                line = line.split()[2]
                                group_access.append(line.replace("@all",""))
                                #group_access.append(line)
                                #group_access.append(line.split()[2])

print '{0:20} {1:25} {2}'.format('TACACSID','Full Name', 'Access')

for x in range(len(user_name)):
        print '{0:20} {1:25} {2}'.format(user_name[x], full_name[x], group_access[x])

print '\nDate Generated: ' + i.strftime('%d/%m/%Y - %H:%M:%S')

searchfile.closed

下面是我得到的错误

回溯(最近一次呼叫最后一次): 文件“/read_tacacs.py”,第20行,在 全名.append(line.split()[3]+''+line.split()[4]) 索引器:列表索引超出范围


任何帮助都会非常感激

#!/usr/bin/python
# v0.1 read_tacacs.py
# Read tac_plus.cfg file and grep username/full_name/access type into column list.

from datetime import datetime

i = datetime.now()

user_name = []
full_name = []
group_access = []


with open('/usr/local/etc/tac_plus.cfg', 'r') as searchfile:
        for line in searchfile:
                if 'user =' in line:
                        user_name.append(line.split()[2])
                if 'Full Name:' in line:
                        full_name.append(line.split()[3]+ ' ' + line.split()[4])
                if 'member' in line:
                        if '@all' in line:
                                line = line.split()[2]
                                group_access.append(line.replace("@all",""))
                                #group_access.append(line)
                                #group_access.append(line.split()[2])

print '{0:20} {1:25} {2}'.format('TACACSID','Full Name', 'Access')

for x in range(len(user_name)):
        print '{0:20} {1:25} {2}'.format(user_name[x], full_name[x], group_access[x])

print '\nDate Generated: ' + i.strftime('%d/%m/%Y - %H:%M:%S')

searchfile.closed
先谢谢你

Ravi

这意味着文件/usr/local/etc/tac_plus.cfg中的行匹配“'Full Name:”,不能包含第3行或第4行元素。您可能需要检查文件/usr/local/etc/tac_plus.cfg以进行调查