Operating System - HP-UX
1821980 Members
3061 Online
109638 Solutions
New Discussion юеВ

Re: PERL scripting problem

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

PERL scripting problem

Hi,
I have the following PERL script attached in this message. It changes the encrypted passwd field in the /etc/passwd file by reading the newly generated encrypted passwd from another file. Note also that I'm changing passwds not just for HP 11 machines, but also Solaris, AIX and Linux.

This posting is similar in its description on the message which I posted about 1 week ago. At that time, I was looking for a AWK/SED script to solve the problem.

However, I decided to give it a shot with PERL to change the passwd entry in /etc/passwd.

But since I'm a newbie in PERL, my problem now is that I'm not sure how I could write the newly generated passwd into the /etc/passwd file.

I do know how to use PERL's substitution operator to change the passwd with the new passwd, but I do not know how to write those changes in /etc/passwd file.

Could someone help me out with my script attached?
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor
Solution

Re: PERL scripting problem

Strong advises:

1. use strict
2. use warnings (for perl 5.005_03 and older use -w)
3. check all your open and close calls
4. use $^O for the OS name instead of unreliable external commands. I don't know the $^O for sunos and linux, so check that yourself)
5. use localtime and time instead of external date command

Tips:

1. use modules when writing cross OS scripts (when portability is an issue in general) instead of external commands
2. keep your lines within 80 characters wide
3. use lexicals wherever possible
4. initialize all your variables

Rewritten script attached

for your core question: unlink might be your clue
Enjoy, Have FUN! H.Merijn
Rick Beldin
HPE Pro

Re: PERL scripting problem

Personally, I would take the safe method:

- open passwd for reading
- open a NEW file for writing
- save the old passwd file
- move the old passwd out of the way
- move the new one into place

This way you end up with a backup in case you have some problem (famous last word - "This has gotta work").

I'd especially recommend something like for the passwd file. You'll be scrambling if your code has a bug.

print "Filename";
$name = <>;
$outfile = "newfile";

chomp $name;

open(HANDLE, "<$name");
open(OUTFILE ">$outfile");
while ()
{
# do stuff
print OUTFILE;
}

close (HANDLE);
close (OUTFILE);

mv $name $name.save
mv $outfile $name



Necessary questions: Why? What? How? When?
H.Merijn Brand (procura
Honored Contributor

Re: PERL scripting problem

Rick, his script *does* make a backup, with a timestamp. Look at the start of the script.

Only safety issue you could add is to launch pwck before moving the old one out of the way.
Enjoy, Have FUN! H.Merijn
Rick Beldin
HPE Pro

Re: PERL scripting problem

Whoops. Didn't see the attachment paperclip.
Necessary questions: Why? What? How? When?