您的位置:首页 > 运维架构 > Shell

得到linux下用户名、id、shell(分别返回列表和字典)

2012-02-09 19:25 260 查看
用笨办法实现(在python 2.4下可以用):

返回列表:

#! /usr/bin/env python
#-*- coding:utf-8 -*-

''' 概述:从/etc/passwd获取系统用户名、用户ID、shell,以列表形式返回
输出:['root, 0, /bin/bash', 'bin, 1, /sbin/nologin', ...]

Create data: 2012-02-09
Version: 1.0
Author: 沈涛

'''

import platform

def getUserName(UserType):
user_list = []

OSType = platform.system()

if (OSType == "Linux"):
fp = open('/etc/passwd').readlines()
for line in fp:
user_list = "%s, %s, %s" % (line.split('\n')[0].split(':')[0],
line.split('\n')[0].split(':')[2],
line.split('\n')[0].split(':')[6])

elif (OSType == "Windows"):
print "Windows System"

return user_list


返回字典:

#! /usr/bin/env python
#-*- coding:utf-8 -*-

''' 概述:从/etc/passwd获取系统用户名、用户ID、shell,以字典形式返回
输出:{'sync': ['5', '/bin/sync'], 'gg': ['506', '/bin/bash'], ...}

Create data: 2012-02-09
Version: 1.0
Author: 沈涛

'''

import platform

def getUserName():
#user_list = {}
item = {}

OSType = platform.system()
print OSType

if (OSType == "Linux"):
fp = open('/etc/passwd').readlines()
for line in fp:
item1 = line.split('\n')[0].split(':')[0]
item2 = line.split('\n')[0].split(':')[2]
item3 = line.split('\n')[0].split(':')[6]
item[item1] = [item2, item3]

elif (OSType == "Windows"):
print "Windows System"

print item

if __name__ == "__main__":
getUserName()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐