I am looking to parse an /etc/passwd file and take out only the users within a range of UIDs. I am not very good with functions it seems.
I found the following function, but can not seem to make it work. from how to make PHP lists all Linux Users?
function getUsers() {
$result = [];
/** @see http://php.net/manual/en/function.posix-getpwnam.php */
$keys = ['name', 'passwd', 'uid', 'gid', 'gecos', 'dir', 'shell'];
$handle = fopen('/etc/passwd', 'r');
if(!$handle){
throw new \RuntimeException("failed to open /etc/passwd for reading! ".print_r(error_get_last(),true));
}
while ( ($values = fgetcsv($handle, 1000, ':')) !== false ) {
$result[] = array_combine($keys, $values);
}
fclose($handle);
return $result;
}
Now I believe I need only put the function in the page then later call it with
getUsers()
This is still not a complete solution however, only a part as I then need to get the UIDs that are within a $min and $max range but if I can not get past the above step it is hard to more on to getting the users in the range mentioned.
Perhaps there is a better way to do this?
UPDATE---------------------------
I got this part,my bad
Now i need to parse the /etc/passwd file
(naysayers go away I do not need your security lecture, as there are NO PASSWORDS in /etc/passwrd. if you knew anything about security you would know that!)
The file looks like this
[24] => Array
(
[name] => lightdm
[passwd] => x
[uid] => 107
[gid] => 115
[gecos] => Light Display Manager
[dir] => /var/lib/lightdm
[shell] => /bin/false
)
[25] => Array
(
[name] => kodi
[passwd] => x
[uid] => 1000
[gid] => 1000
[gecos] => ,,,
[dir] => /home/kodi
[shell] => /bin/bash
)
[26] => Array
(
[name] => saned
[passwd] => x
[uid] => 108
[gid] => 119
[gecos] =>
[dir] => /var/lib/saned
[shell] => /bin/false
)
[27] => Array
(
[name] => statd
[passwd] => x
[uid] => 109
[gid] => 65534
[gecos] =>
[dir] => /var/lib/nfs
[shell] => /bin/false
)
[28] => Array
(
[name] => sshd
[passwd] => x
[uid] => 110
[gid] => 65534
[gecos] =>
[dir] => /var/run/sshd
[shell] => /usr/sbin/nologin
)
[29] => Array
(
[name] => pulse
[passwd] => x
[uid] => 111
[gid] => 121
[gecos] => PulseAudio daemon,,,
[dir] => /var/run/pulse
[shell] => /bin/false
)
[30] => Array
(
[name] => rtkit
[passwd] => x
[uid] => 112
[gid] => 123
[gecos] => RealtimeKit,,,
[dir] => /proc
[shell] => /bin/false
)
[31] => Array
(
[name] => avahi
[passwd] => x
[uid] => 113
[gid] => 124
[gecos] => Avahi mDNS daemon,,,
[dir] => /var/run/avahi-daemon
[shell] => /bin/false
)
[32] => Array
(
[name] => hplip
[passwd] => x
[uid] => 114
[gid] => 7
[gecos] => HPLIP system user,,,
[dir] => /var/run/hplip
[shell] => /bin/false
)
[33] => Array
(
[name] => spanish
[passwd] => x
[uid] => 1001
[gid] => 1001
[gecos] => spanish,,,,
[dir] => /home/spanish
[shell] => /bin/bash
)
I think for greatest effeciency is to parse the file once creating a new array . I have set a $minuid and $maxuid that selects the users I want based on the 'uid' key.
WHat I need to get back is a smaller array with ONLY those users with a UID that falls between $maxuid and $minuid then easily be able to get 'name' and 'dir' fields for those relevant users.
I seem kind of lost at how to do this.