<p>我已经编译了工作代码,所以它可能是有用的人看所以。现在有两种有<code>regex</code>和没有<code>regex</code>的解决方案。你知道吗</p>
<p><strong>1)不含<code>regex</code></strong></p>
<pre><code>from subprocess import Popen, PIPE, DEVNULL
##### color for success & Failed code ########
CRED = '\033[91m'
CGRN = '\033[92m'
CEND = '\033[0m'
###############################################
with open(input("Please Enter the userfile: "), "r") as lid:
for line in lid:
line = line.strip()
proc = Popen(['groups', line], stdout=PIPE, stderr=DEVNULL)
myID = proc.communicate()[0].decode('utf-8')
if 'koint' in myID.split():
print(line, CGRN + "Success: " + CEND + "user exists in the group")
else:
print(line, CRED + "Failed: " + CEND + "user doesn't exists in the group")
</code></pre>
<p><strong>2)基于<code>regex</code></strong></p>
<pre><code> from subprocess import Popen, PIPE, DEVNULL
import re
##### color for success & Failed code ########
CRED = '\033[91m'
CGRN = '\033[92m'
CEND = '\033[0m'
################################################
with open(input("Please Enter the userfile: "), "r") as lid:
for line in lid:
line = line.strip()
proc = Popen(['id', line], stdout=PIPE, stderr=DEVNULL)
myID = proc.communicate()[0].decode('utf-8')
if re.search(r'\bkoint\b', myID):
print(line, CGRN + "Success: " + CEND + "User exists in the Group")
else:
print(line, CRED + "Failed: " + CEND + "User does not exists in the Group")
</code></pre>