python创建linux用户,用Python创建Linux用户列表

我正在用Python编写一个脚本,它将查看linux系统上的所有组和所有用户,并向我输出一个文件。在

我有一种情况,如果某个帐户在某个组中,我想自己设置用户ID(在本例中,这是因为该帐户是非用户帐户)

代码如下:#/usr/bin/python

import grp,pwd,os

from os.path import expanduser

destdir = expanduser("~")

destfile = '/newfile.txt'

appname = 'app name'

groupid = ''

userid = ''

#delete old feed file and create file

if os.path.exists(destdir + destfile):

os.remove(destdir + destfile)

print "file deleted...creating new file"

output = open(destdir + destfile, 'w+')

output.write('ACCOUNTID|USERLINKID|APPLICATIONROLE|APPLICATION' + '\n')

else:

print "no file to delete...creating file"

output = open(destdir + destfile, 'w+')

output.write('ACCOUNTID|USERLINKID|APPLICATIONROLE|APPLICATION' + '\n')

#get user/group data for all users non primary groups

#documentation: https://docs.python.org/2/library/grp.html

groups = grp.getgrall()

for group in groups:

groupid = group[2]

print groupid #checking to see if group ids print correctly. Yes it does

for user in group[3]:

if groupid == '33': #Issue is here!

userid = 'qwerty'

print userid #testing var

output.write(user + '|' + userid + '|' + group[0] + '|' + appname + '\n')

问题是:

^{pr2}$

变量“userid”从未设置为它的值,并且在测试时从不打印任何内容。在

组“33”中确实有用户并且存在。我不明白为什么这样不行:(

我有另一段代码可以为用户实现这一点(因为我正在查看主要组和次要组,一旦我弄清楚这一部分,我就可以修复其余部分)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值