Create a shell script to gather user account information and displays the result to administrator


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Create a shell script to gather user account information and displays the result to administrator
# 1  
Old 11-17-2018
Create a shell script to gather user account information and displays the result to administrator

I want to create a shell script to gather user account information and displays the result to administrator.


I have created a script but its showing all the information when i search for username like:

Code:
amit@mx:~$ ./uinfo.sh  amit

Username                  :  amit  [User Id - 1000]
User Info                 : amit,,,

User's Primary Group      :  amit  [Group Id - 1000]
User is Member of Groups  : amit adm cdrom sudo dip plugdev lpadmin sambashare

Home Directory            : /home/amit  [Size Occupied - 162M]
Default Shell             : /bin/bash





I want to use script like 



./uinfo.sh -i <username> Display user ID
./uinfo.sh -g <username> Display user GID


===========================================
#!/bin/sh

if [ "$1" = "" ]
then
        echo
        echo "Usage: $0 USERNAME"
        echo





        echo "Example:  $0 kam"
        echo

        exit 1
fi

Username=`cat /etc/passwd | grep -Ew ^$1 | cut -d":" -f1`

if [ "$Username" = "" ]
then



        echo "Username $1 doesn't exist"
        exit 2
fi

Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`
UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4`
UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1`
UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5`
UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6`
UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7`

UserGroups=`groups $Username | awk -F": " '{print $2}'`
PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '$
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk $
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{p$
HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`

echo
printf "%-25s : %5s  [User Id - %s]\n" "Username" "$Username" "$Userid"
printf "%-25s : %5s\n" "User Info" "$UserInfo"
echo
printf "%-25s : %5s  [Group Id - %s]\n" "User's Primary Group" "$UserPrimaryGro$
printf "%-25s : %5s\n" "User is Member of Groups" "$UserGroups"
echo
printf "%-25s : %5s  [Size Occupied - %s]\n" "Home Directory" "$UserHomeDir" "$$
printf "%-25s : %5s\n" "Default Shell" "$UserShell"
echo

=========================================


can anyone help...

Moderator's Comments:
Mod Comment Added code tags...
# 2  
Old 11-17-2018
Hello!

In case you forgot to read the forum rules, here is quick copy.

Quote:
RULES OF THE UNIX AND LINUX FORUMS



(1) No flames, shouting (all caps), sarcasm, bullying, profanity or arrogant posts.

(2) No negative comments about others or impolite remarks. Be patient. No BSD vs. Linux vs. Windows or similar negative threads.

(3) Refrain from idle chatter that does not contribute to the knowledge base. This does not apply to the forums in The Unix Lounge which are for off-topic discussions.

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

(5) Search the forums database with your keywords before asking questions.

(6) Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

(7) No job postings from headhunters or recruiters except via display advertising. See Advertising in The UNIX and Linux Forums for information on buying display ads.

(8) Use Code Tags around all code and data fragments in posts.

(9) Edit your posts if you see spelling or grammar errors (don't write in cyberchat or cyberpunk style). English only.

(10) Don't post your email address and ask for an email reply. Don't send a private message with a technical question. The forums are for the benefit of all, so all Q&A should take place in the forums.

(11) Post questions with descriptive subjects. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

(12) These are not hacker boards so hacker related posts will be promptly deleted or moderated.

(13) The forum administrators reserve the right to prune, move or edit posts that do not adhere to the rules or are technically inaccurate.

(14) The forum administrators reserve the right to remove users or change their posting status to read only without notice if any rules are not followed.

(15) No smoking in the forums.


Cheers.

The UNIX and Linux Forums
Moderator's Comments:
Mod Comment Please read the rules and report per forum rules and guidelines.
This User Gave Thanks to Neo For This Post:
# 3  
Old 11-17-2018
Quote:
Originally Posted by amit1986
I want to use script
It would help to know which shell and which OS you are using. As it is my crystal ball with which i used to look into your computer usually is in repair right now.

Quote:
Originally Posted by amit1986
like

./uinfo.sh -i <username> Display user ID
./uinfo.sh -g <username> Display user GID
You have to put in some mechanism to understand commandline options to do that. The usual way is to use getopts to accomplish this. I suggest to read it man page. I have showcased its usage in this thread and script which you might want to use as a starter.

Quote:
Originally Posted by amit1986
Code:
Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`

This (and all the similar lines) contains several mistakes at once:

First, this is a so-called "useless use of cat" or "UUOC". The error is so "famous" that it even has its own name (and you can look it up in google): utilities like grep do not need a cat because they can either take input from stdin or a filename themselves. The following lines do absolutely the same but the second variant uses one process less to accomplish it:

Code:
cat /some/file | grep "<regexp>"
grep "<regexp>" /some/file

Second, you should NOT use backticks any more, regardless of which shell you use. Even the oldest shell running today understands modern POSIX process substitution:

Code:
var=$(process1 | process2 | ... )

and if a shell doesn't: put it in a museum where it belongs but don't use it any more. Get something from this (or at least the last) century to use instead of an pre-bronze-age antiquity.

Third: you read the file /etc/passwd over and over again for each bit of information you look for:

Code:
Username=$(cat /etc/passwd | grep ..... )
Userid=$(cat /etc/passwd | grep ..... )
UserPrimaryGroupId=$(cat /etc/passwd | grep ...... )
....

you can get all the info in one pass: you know that /etc/passwd is organised in fields separated by a colon character (":"), yes? You can use the field-splitting ability of the shell by telling it which character to split at. Per default this is a space, but it can be changed by changing the IFS (internal field separator) variable:

Code:
IFS=":" read Username junk UserId UserPrimaryGroupId ..... < $( grep "username" /etc/passwd )

This will set all the variables at once: the first field goes to "Username", the second to "junk" (you seem not to use it, i make a habit of naming not-needed info that way), the third one to "UserId", etc.. Be sure to put an additional "junk" at the end of the variable list for any piece of information at the end of the line you are eventually not interested in because all "left over" parts of the input goes to the last variable in the list.

One last word of caution: you use:

Code:
grep -Ew ^$user

to select the respective line in /etc/passwd. This may create problems in case you i.e. have two users named "joe" and "joex". You should also protect your script against injections of wildcards - suppose i enter "." as a username to search for. Your grep-command would select any user and your subsequent script (which relies on exactly one user being picked) would break. The following will reduce this risk:

Code:
fgrep "^${user}:" /etc/passwd

I hope this helps.

bakunin

Last edited by bakunin; 11-18-2018 at 08:57 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for user account Creation

Hi Folks, I had a request to create the user request. Between, I just write a script a create, Update Geos, and update the password. My script as below: The error message, what I am getting is all the users are updated with the same Goes value.. #!/bin/bash for i in `cat users.txt`;do... (2 Replies)
Discussion started by: gsiva
2 Replies

2. UNIX for Dummies Questions & Answers

How can I use my code to gather information from a file in a completely different directory?

I need my code to compare two different files that are in two completely different directories, How can I do this? So for example, my code will look at file1 which is in my home directory, and compare the files with those from file2 that is in /abc/adf/adr/afc/adf/file2... does that make sense? (1 Reply)
Discussion started by: castrojc
1 Replies

3. Solaris

Help me create new user account

I want create user. That user should be login to any server without asking password. How? tell me in detail. :wall: (3 Replies)
Discussion started by: Navkreddy
3 Replies

4. Shell Programming and Scripting

Shell script for user login information.

Hi Gurus, I need help in writing a script which should say which user has used or logged in in the server from past one month using FTP or TELNET and the output should be of the form Username Service NumberofTimes Date. Thanks in Advance. ---------- Post updated at 04:01 PM... (1 Reply)
Discussion started by: rama krishna
1 Replies

5. AIX

how to gather HMC information

does somebody know how to gather HMC information? It looks like there is some tool can gather HMC configuration to a html file and make them as a xx.tar.gz file? (2 Replies)
Discussion started by: rainbow_bean
2 Replies

6. AIX

Using NIM to gather system information

Hi, Need help if its possible to use NIM server to gather information. Basically, we need to gather firmware version and oslevel for environment wide servers (these servers are connected thru nim). I understand you can use NIM script resource to trigger the script but don't know if possible to... (3 Replies)
Discussion started by: depam
3 Replies

7. Solaris

How to see the root information from user loging account?

Hi friends when ever user tried to loging to the server from the user account.we can see the from who -u command.this was fine shut@erpqas $ who -u ipadmin pts/1 Mar 18 16:05 old 157 (10.5.23.74) ipadmin pts/3 Mar 19 08:29 old 11076 ... (3 Replies)
Discussion started by: tv.praveenkumar
3 Replies

8. Shell Programming and Scripting

Create new user account and password in shell script

I am trying to create a shell script that will: check if a specific user already exists if not, create a specific group and create the user in that group assign a password to that user, where the password is passed in as a parameter to the script The problem that I need help with is 3 on... (4 Replies)
Discussion started by: killuane
4 Replies

9. Filesystems, Disks and Memory

script to create multiple instances of a user account across LPAR's

My company has about 40 databases with each database in a different logical partition. Presently the SysAdmin person says it is necessary to create a user profile (login and password for each instance of databases on each LPAR. 1. Is it necessary that the user must be created in each LPAR? 2.... (1 Reply)
Discussion started by: kcampbell
1 Replies

10. HP-UX

view user account information

How can dump the user account detail? like how long need to change password, password naming policy, how many times will lock account if login failed.. thk a lot (0 Replies)
Discussion started by: zp523444
0 Replies
Login or Register to Ask a Question