I have a list of various su
commands. All the commands use the full path to the su
command.
And the commands may or may not have a username and may or may not have the -
option.
Also there maybe options and arguments after the username like /usr/bin/su aa*
or /bin/su - squid *
or /bin/su - mapr -c "/usr/bin/hadoop fs -mkdir /user/*"
.
Here is what I have tried and I didn't get very far before I ran into trouble.
for c in su_commands.all()[0:10]:
...: print(c.name)
...: m = re.search('/su\s*[-]\s*(\S*)', c.name).group(1)
...: print(m)
...:
//bin/su - audituser.gen
audituser.gen
//usr/bin/su - hradm
hradm
/apps/su - capital
capital
/apps/su - cscdm
cscdm
/apps/su - invadm
invadm
/bib/su sapbridg
Traceback (most recent call last):
File "/apps/sfo_rcecala/sfo_rcecala/env3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-39-788d11c0e2c2>", line 3, in <module>
m = re.search('/su\s*[-]\s*(\S*)', c.name).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
I thought that have [-]
in my regex would make the -
optional.
Thanks Barmar. I have updated my regex and now I am getting further.
for c in su_commands.all()[0:10]:
...: print(c.name)
...: m = re.search('/su\s*\-?\s*(\S*)', c.name).group(1)
...: print(m)
...:
//bin/su - audituser.gen
audituser.gen
//usr/bin/su - hradm
hradm
/apps/su - capital
capital
/apps/su - cscdm
cscdm
/apps/su - invadm
invadm
/bib/su sapbridg
sapbridg
/bin//su - hdpapjpa
hdpapjpa
/bin/su oabifstg
/bin/su xxcconx
xxcconx
/bin/su xxswaadm
xxswaadm
But as you can see I missed the user oabifstg
.