LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2011, 04:38 PM   #1
krymsunmortis
Member
 
Registered: Jan 2003
Location: Fort Worth Tx
Distribution: Slackware RedHat Mandrake BackTrack
Posts: 31

Rep: Reputation: 15
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.
 
Old 01-19-2011, 05:01 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You should just need to use grep to see if any lines match the username.
 
Old 01-19-2011, 05:02 PM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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.
 
Old 01-19-2011, 05:20 PM   #4
krymsunmortis
Member
 
Registered: Jan 2003
Location: Fort Worth Tx
Distribution: Slackware RedHat Mandrake BackTrack
Posts: 31

Original Poster
Rep: Reputation: 15
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.
 
Old 01-19-2011, 05:54 PM   #5
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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?
 
Old 01-19-2011, 06:30 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by krymsunmortis View Post
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.
 
Old 01-19-2011, 09:10 PM   #7
DMcCunney
LQ Newbie
 
Registered: Feb 2009
Posts: 16

Rep: Reputation: 2
Quote:
Originally Posted by krymsunmortis View Post
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
 
Old 01-20-2011, 06:13 PM   #8
krymsunmortis
Member
 
Registered: Jan 2003
Location: Fort Worth Tx
Distribution: Slackware RedHat Mandrake BackTrack
Posts: 31

Original Poster
Rep: Reputation: 15
Thank you

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
give password with command /usr/sbin/passwd jonaskellens Linux - Newbie 8 09-25-2009 08:34 AM
running script when inserting SD card novakane Slackware 6 09-18-2009 12:10 PM
Inserting into Text Files From Bash Script userLin Programming 3 03-30-2009 07:16 AM
meaning of suid on /usr/bin/passwd raj k yadav Linux - Newbie 1 01-08-2009 10:52 AM
inserting script in system install Bobbyd4 Slackware 2 02-21-2007 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration