Search code examples
pythonpycharmsubprocess

Using getent passwd {1001..} as a subprocess


I am writing a Python script that creates 33 users through the "useradd" subprocess in Ubuntu. After that I wish to print out the added users by using another subprocess, perferably getent. I want to add {1001..1033} so that the print omits the uneeded information and only shows the users based on UID but do not know how to do it.

I had tried writing "subprocess.run(["getent", "passwd"], {1001..1033})", but that immediately sends of warning signs in Pycharm. Without the "{1001..1033}" the subprocess works, but prints walls of redundant information.

EDIT: Solved thanks to leviant pied.


Solution

  • This should work:

    >>> subprocess.run(['getent', 'passwd'] + list(map(str, range(1001, 1033 + 1))))
    CompletedProcess(args=['getent', 'passwd', '1001', '1002', '1003', '1004', '1005', '1006', '1007', '1008', '1009', '1010', '1011', '1012', '1013', '1014', '1015', '1016', '1017', '1018', '1019', '1020', '1021', '1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1031', '1032', '1033'], returncode=2)