LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-23-2013, 07:33 AM   #1
scottmusician
Member
 
Registered: Jul 2011
Location: Melbourne, AU
Distribution: Centos 5
Posts: 43

Rep: Reputation: Disabled
Cool perl script to rationalize /etc/groups


Hi all,

I am trying to write a simple perl script to optimize my /etc/groups, to make sure that it doesn't contain any members that don't have corresponding /etc/passwd entries.

here's what I have so far, but it all seems a little cumbersome:
Code:
#!/usr/local/bin/perl

open(F, "/etc/passwd");
@passwd = <F>;
chop(@passwd);
open(F, "/etc/group");
@group = <F>;
chop(@group);

for ($i=0; $i<$#group; $i++) {
        $line = $group[$i];
        $line =~ /^(..*):(..*):(..*):(..*)$/;
        ($group, undef, undef, $members) = ($1, $2, $3, $4);
        @members = split(/,/, $members);
        foreach $m (@members) {
                printf("%s\n", $m) unless grep(/^$m:/, @passwd);
        }
}
Is there a neater way of doing this in perl?

(PS: it needs to be in perl, as it's also for my programming class )

Last edited by scottmusician; 06-23-2013 at 08:35 AM.
 
Old 06-23-2013, 09:25 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,507

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
yes, you can try to use a hash:
store all the usernames in a hash, %users (values are not important, we need to keys only).
and you can write your search as: grep (! exists $users{$_}, split(/,/, $members));
Not tested, there can be other possibilities, for example using map.
 
Old 06-25-2013, 02:05 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,241

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
1. always use -w & strict
Code:
#!/usr/bin/perl -w
use strict;
2. use 3 arg open and always close files you've opened asap (especially key ones like passwd!) and check open/close worked ok
Code:
open(KFILE, "<", "kfile.txt" ) or die "Can't open kfile: $!\n";
@passwd = <F>;
chomp(@passwd);
close(KFILE) or die "Can't close kfile: $!\n";
& prefer chomp() to chop().

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
http://www.tizag.com/perlT/index.php
 
  


Reply

Tags
groups, hosts, networking, perl, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dnsmasq: how to prevent upstream queries for hosts in /etc/hosts? catkin Linux - Networking 2 02-18-2013 10:50 AM
Script for hosts, numbers of hosts and users connected to squid server arunabh_biswas Programming 5 08-28-2010 04:11 AM
Bash/Perl script - provide list of hosts to choose from and ssh to chosen host loadedmind Programming 4 04-07-2010 01:19 PM
can't restrict sshd access through hosts.allow and hosts.deny but was working earlier farhan Linux - Security 4 04-18-2008 07:41 AM
/etc/hosts.deny/hosts.allow have no effect on sshd access bganesh Linux - Security 4 05-04-2006 08:06 PM


All times are GMT -5. The time now is 03:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration