Skip to content

Commit f0a2ef6

Browse files
committed
WIP: facts.server.Users: go through libnss
1 parent 3188ad8 commit f0a2ef6

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pyinfra/facts/server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,13 @@ class Users(FactBase):
441441
}
442442
"""
443443

444-
def command(self):
445-
return """
444+
def requires_command(self) -> str:
445+
return "getent"
446446

447-
for i in `cat /etc/passwd | cut -d: -f1`; do
448-
ENTRY=`grep ^$i: /etc/passwd`;
447+
def command(self) -> str:
448+
return """
449+
for i in `getent passwd | cut -d: -f1`; do
450+
ENTRY=`getent passwd | grep ^$i:`;
449451
LASTLOG_RAW=`(lastlog -u $i 2> /dev/null || lastlogin $i 2> /dev/null)`;
450452
LASTLOG=`echo $LASTLOG_RAW | grep ^$i | tr -s ' '`;
451453
PASSWORD=`grep ^$i: /etc/shadow | cut -d: -f2`;

tests/facts/server.Users/mixed.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"freebsd:*:1001:1001:FreeBSD:/home/freebsd:/bin/sh|freebsd|freebsd|freebsd pts/0 10.1.10.10 Thu Aug 31 05:37:58 2023|$bsdpw$",
99
"freebsdnologin:*:1001:1001:FreeBSD NoLogin:/home/freebsdnologin:/bin/sh|freebsdnologin|freebsdnologin||"
1010
],
11-
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n LASTLOG_RAW=`(lastlog -u $i 2> /dev/null || lastlogin $i 2> /dev/null)`;\n LASTLOG=`echo $LASTLOG_RAW | grep ^$i | tr -s ' '`;\n PASSWORD=`grep ^$i: /etc/shadow | cut -d: -f2`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG|$PASSWORD\";\n done",
11+
"command": "for i in `getent passwd | cut -d: -f1`; do\n ENTRY=`getent passwd | grep ^$i:`;\n LASTLOG_RAW=`(lastlog -u $i 2> /dev/null || lastlogin $i 2> /dev/null)`;\n LASTLOG=`echo $LASTLOG_RAW | grep ^$i | tr -s ' '`;\n PASSWORD=`grep ^$i: /etc/shadow | cut -d: -f2`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG|$PASSWORD\";\n done",
12+
"requires_command": "getent",
1213
"fact": {
1314
"root": {
1415
"home": "/root",
@@ -104,4 +105,4 @@
104105
"password": ""
105106
}
106107
}
107-
}
108+
}

0 commit comments

Comments
 (0)