ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
Last edited by Nominal Animal; 03-21-2011 at 06:15 AM.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.