LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inserting usr into /etc/passwd via script (https://www.linuxquestions.org/questions/programming-9/inserting-usr-into-etc-passwd-via-script-857381/)

krymsunmortis 01-19-2011 04:38 PM

inserting usr into /etc/passwd via script
 
ok I have an issue. I need to either locate a script that is similar to what I am needing or figure out a better way of doing what I need.
I have mutiple shops with AIX unix servers, using ksh with virtual terminals that connect. since these are on an internal network we have them connecting to the server as either usr01, usr02, etc.
what I need to do is add 15 user's ranging from usr01-usr15 into /etc/passwd each usr is identical in such that each line contains
Code:

usr01::0:0::/usr/tops:/bin/ksh
only difference is the usr# changes.
I wrote a script where I was just adding these all to the /etc/passwd but now I have been tasked with adding them to these shops but with out any duplicates.
is there any way to have a script check the file to see if the usr# exists and if so proceed to the next number and then input the usr#::0:0::/usr/tops:/bin/ksh into the file?

any assistance or even a shove off teh correct cliff will help.
Thanks in advance.

Snark1994 01-19-2011 05:01 PM

You should just need to use grep to see if any lines match the username.

AlucardZero 01-19-2011 05:02 PM

Editing /etc/passwd is the wrong way to do it. Are you even touching /etc/security/passwd?

Use useradd instead. Fill out its switches and write a script with 15 useradd lines (or a loop from 01 to 15). useradd will return an error when the user exists, solving your duplicate problem. Just make sure that your script doesn't quit on the first error. useradd will also PROPERLY add users to a system.

krymsunmortis 01-19-2011 05:20 PM

Quote:

Editing /etc/passwd is the wrong way to do it. Are you even touching /etc/security/passwd?
is the way it is setup there is no password what so ever in the file due to this just for the workstations to connect to the system once connected we have them typing their own passwords for all functions they do.
I cant edit the switchs on over 800 shops. I just need to be able to enter those lines into the /etc/password with out causing duplicates.
I send the script out to all the shops via a SENDLIST.sh and it then runs the script on the system at the next cron job processing which is every 15 minutes on our systems. the usr01, usr02 are just the way teh workstation connects to the server. once it connect to the server it auto opens our menu driven software so there is no need for worry or concern of it not being the correct way or unsecure way.


Quote:

You should just need to use grep to see if any lines match the username.
yes I can use a grep to check for any duplicates however again I am sending this out via mass push to mutiple locations.

so how would you recommend using grep to have the script check the file and see if there is an entery and if there isnt then to add one.

AlucardZero 01-19-2011 05:54 PM

So.. if
Quote:

each usr is identical in such that each line contains ... only difference is the usr# changes
, then what is wrong with one script containing:
Code:

useradd -s /bin/ksh -d /usr/tops user01
useradd -s /bin/ksh -d /usr/tops user02
useradd -s /bin/ksh -d /usr/tops user03
#etc

that you run on all hosts?

Nominal Animal 01-19-2011 06:30 PM

Quote:

Originally Posted by krymsunmortis (Post 4230765)
what I need to do is add 15 user's ranging from usr01-usr15 into /etc/passwd each usr is identical in such that each line contains
Code:

usr01::0:0::/usr/tops:/bin/ksh
only difference is the usr# changes.

Can you use awk? This awk script outputs the modified /etc/passwd to standard output. The first line skips all existing usr lines, the second prints normal lines, and the third line generates the needed users. I tried to make it as understandable as possible.
Code:

awk '/^usr[0-9][0-9]*:/ { next }
    { print $0 }
END { for (i=1; i<=15; i++) printf("usr%02d::0:0::/usr/tops:/bin/ksh\n", i) }
' /etc/passwd

Pipe the output to a safe temporary file. If there was no error, make sure the temporary file is owned by root and has mode 0644 (-rw-r--r--), then move it over the old passwd file.

Without awk, you'll have to do it in two phases. First eliminate any old usr users via
Code:

sed -e '/^usr[0-9][0-9]*:/ d' /etc/passwd
which of course outputs the result to standard output. Direct that to a file, append the fifteen users, and then replace the original /etc/passwd file (if no errors occurred).

Hope this helps,
Nominal Animal

DMcCunney 01-19-2011 09:10 PM

Quote:

Originally Posted by krymsunmortis (Post 4230765)
ok I have an issue. I need to either locate a script that is similar to what I am needing or figure out a better way of doing what I need.
I have mutiple shops with AIX unix servers, using ksh with virtual terminals that connect. since these are on an internal network we have them connecting to the server as either usr01, usr02, etc.
what I need to do is add 15 user's ranging from usr01-usr15 into /etc/passwd each usr is identical in such that each line contains
Code:

usr01::0:0::/usr/tops:/bin/ksh
only difference is the usr# changes.
I wrote a script where I was just adding these all to the /etc/passwd but now I have been tasked with adding them to these shops but with out any duplicates.
is there any way to have a script check the file to see if the usr# exists and if so proceed to the next number and then input the usr#::0:0::/usr/tops:/bin/ksh into the file?

It's been a while since I had to do this, and that was for Solaris, but I assume AIX works largely the same way. The useradd command does the heavy lifting, updating /etc/passwd and the /etc/shadow files. The format is useradd [options] username.

A script might look like this:
Code:

for num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
do
    if grep /etc/passwd "usr$num"
    then :    # do nothing
    else
        useradd -c "<comment>" -d /usr/tops -g <group> -s /bin/ksh usr$num
    fi
done

The above is untested - I'm in Windows at the moment - but illustrates the idea.

This loops through the possible combinations of usr01 through usr15, does a grep on /etc/passwd to see if the id already exists, and if not creates it, adding a user defined comment (like "virtual terminal id"), assigns the id to a group of your choice, and sets the login shell as ksh.
______
Dennis

krymsunmortis 01-20-2011 06:13 PM

Thank you
 
Thank you to both Nominal Animal and DMcCunney. That looks like what I am needing thank you


All times are GMT -5. The time now is 05:15 PM.