Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Parsing a line of text

by TCPDump (Initiate)
on Mar 13, 2013 at 16:57 UTC ( #1023256=perlquestion: print w/replies, xml ) Need Help??
TCPDump has asked for the wisdom of the Perl Monks concerning the following question:

First off, I am not attempting to crack passwords here....

As everyone knows most Unix systems put their passwords in a file usually /etc/shadow.

Each line has 9 fields including the fields that say when the password was last changed, and how long the password is good for.

Several of our poor old abused Unix boxes have over 2,000 user names on it but we think only about 500 use the system. I am looking for a way to take the shadow file and:

Read the /etc/shadow file line by line (9 fields seperated by a ':' )

Take the contents of field 1 which is the user name

Take the first 3 characters of the second field which can be a * meaning locked, or a !! meaning expired, or a $X$ with type of encryption used.

Take the third field which is the day the password was last changed, convert it to a real date since it is the number of days since Jan 1 1970

Take the value of the fifth field and add it to the third field and convert it to a date which would then tell me the date of the next password change

Then display the user name, account status, date of last password change and the date of the next one. Better yet, some sort of notation that the account is expired if it is.

I have been requested to write a script in my spare time that will provide some diagnostics on a box so this is the first part. Since I am not anywhere ner a programmer, I am pretty sure I will have more questions in the future

Thanks

TCPDump

Replies are listed 'Best First'.
Re: Parsing a line of text
by pvaldes (Chaplain) on Mar 13, 2013 at 17:14 UTC

    in pseudocode

    # we read the /etc/shadow to a filehandle open (my $filehandle, '<', '/etc/shadow') or die $!; while (<$filehandle>){ # we read line by line now chomp; # and split the line by fields my ($name, $passwd, $field3, $field4,... ETC until $field8) = split /: +/ $_; if ($passwd = '*'){do something...} elsif ($passwd = '!'){do other...} } # we close the while loop close $filehandle;

    Updated: you don't need to double the !! probably

    Updated again: missed a semicolon after die

Re: Parsing a line of text
by toolic (Bishop) on Mar 13, 2013 at 17:12 UTC
Re: Parsing a line of text
by McA (Priest) on Mar 13, 2013 at 17:21 UTC

    Hi,

    there is something which makes me laughing:

    "I have been requested to write a script in my spare time [...]"
    That means someone is thinking you can do this, but in the same sentence you say:
    "[...] Since I am not anywhere ner a programmer [...]"
    That lets me ask: Why haven't you said: I'm not a programmer, so I'm not the right person to do this. And by no means in my spare time forcing me to ask someone else programming the script?

    Best regards
    McA

      I have said that to my management, but no one on our team is anywhere near a programmer....we deal with network, information and system security...unfortunantly the *nix team doesn't want to do this, so systems security such as this falls under our umbrella.

      I do, however, see the need for quick scripting languages so I am going to learn Perl as my time permits.

      As for asking others, useful answers "usually" come from people that know how to do what is needed and someone like me could end up at a result that is neither elegant or takes into account 'known' problems. If I learn from the people that know how dto do what is needed then I learn the correct way to do it as well as...I can read proper code

      TCP

Re: Parsing a line of text
by kcott (Chancellor) on Mar 13, 2013 at 17:34 UTC

      Ken

      Thanks for the point in the right direction. That is what I was looking for, some form of starting point which you gave me by telling me what is needed. I had not thought of Date::Manip, so most of the rest of the code should be farly easy...I've used the other commands before in other scripts

      Thanks again...TCP

Re: Parsing a line of text
by trwww (Priest) on Mar 13, 2013 at 21:41 UTC
    Could you provide some sample input?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023256]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2018-08-17 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (174 votes). Check out past polls.

    Notices?