Operating System - HP-UX
1846627 Members
2279 Online
110256 Solutions
New Discussion

Re: Scripting: Please help

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Scripting: Please help

Hi All,

I have a project to implement a SecureID on all of my HP servers. A part of the project is to create a script to replace the shell of certain users on /etc/passwd with file called /opt/ace/sdshell.

For example: I have a file called users as below "
user2
user3
user5

And I have /etc/passwd as below :
user1:*:778:406:user1 123 for testing only:/home/user1:/bin/sh
user2:*:780:406:user2 124 for testing only:/home/user2:/bin/sh
user3:*:781:406:user3 125 for testing only:/home/user3:/bin/sh
user4:*:782:406:user4 126 for testing only:/home/user4:/bin/sh
user5:*:783:406:user5 127 for testing only:/home/user5:/bin/sh
user6:*:784:406:user6 128 for testing only:/home/user6:/bin/sh

So only the shell's of all the users under file users will be replaced with /opt/ace/sdshell. So the result should be as below :

user1:*:778:406:user1 123 for testing only:/home/user1:/bin/sh
user2:*:780:406:user2 124 for testing only:/home/user2:/opt/ace/sdshell
user3:*:781:406:user3 125 for testing only:/home/user3:/opt/ace/sdshell
user4:*:782:406:user4 126 for testing only:/home/user4:/bin/sh
user5:*:783:406:user5 127 for testing only:/home/user5:/opt/ace/sdshell
user6:*:784:406:user6 128 for testing only:/home/user6:/bin/sh

Pls help. High score will be given.

Thanks.
Negara
Santos
6 REPLIES 6
Hein van den Heuvel
Honored Contributor

Re: Scripting: Please help


try the following perl script:

open (USERS,"while () { chop; $user{$_} = 1 }
open (PASSW,"
while () {
@words = split (/:/);
$words[6] = "/opt/ace/sdshell\n" if ($user{$words[0]});
print join(":",@words);
}


I'm sure it can be done much shorter, but this works.

Open user list
Remember each user in associative array
Open passwd
Read all
split, based on colon as seperator
replace 7th word (shell) if 1st word is a remembered user
print list joined by colons

hth,
Hein.

curt larson_1
Honored Contributor
Solution

Re: Scripting: Please help

cat yourFile |
while read userN
do
chsh $userN /opt/ace/sdshell
done
curt larson_1
Honored Contributor

Re: Scripting: Please help

to use the chsh command, if the file /etc/shells exists, the new login shell must be listed in that file
Sanjay Kumar Suri
Honored Contributor

Re: Scripting: Please help

Check the following:

grep -f users passwd > temp
sed "s/\/bin\/sh/\/opt\/ace\/sdshell/" temp > unsorted
grep -v -f users passwd >> unsorted
sort unsorted > sdshell

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Dewa Negara_4
Regular Advisor

Re: Scripting: Please help

Hi All,

Thanks alot for your really great help. It looks fine for me now. All those are running well

I prefer to use the shell one instead perl because lack of knowlegde on this.

Thanks.
Negara
Santos
Muthukumar_5
Honored Contributor

Re: Scripting: Please help

Hai.

I have attached the shell script which will do your requirement.

You have to specify the userlists in that script to be changed manually.

Modified file will be there as /etc/passwd_mod. Change the permission as like /etc/passwd.

I have tested with your example contents.

Regards,
Muthukumar
Easy to suggest when don't know about the problem!