1826446 Members
4060 Online
109692 Solutions
New Discussion

Help in a script

 
SOLVED
Go to solution
Waqar Razi
Regular Advisor

Help in a script

I have 3 passwd files from three servers namely passwd1, passwd2, passwd3. Some users are present only on one server, some are present in two and some of them are present in 3 passwd files. If they are present in more than 1 server, they have the same user name. I want to modify the following script:

sort passwd1 passwd2 passwd3 | awk -F: '
BEGIN { getline; name = $1; uid = $3 }
{
if ($1 == name) {
uid = uid " " $3 # concat
} else {
print name, uid # print last
name = $1; uid = $3
}
}
END { print name, uid } '

It gives output like the following:
ufeabjp 16128 16128 16128
ufeabln 16096 16096 16096
ufeabms 16118 16118 16118


This script prints username followed by his ids in three servers. I want to add some more information to it like Real name, description and if possible I want to add the time and date when the user logged in last time.

The desired output is:

username userid userid userid Name Desc lastlogin

Can any one help me in modifying the script?

11 REPLIES 11
TTr
Honored Contributor

Re: Help in a script

What you are asking is ambiguous. How can you add a name/desc if a user appears in all 3 passwd files? Do you know that the real name/desc is the same in all 3 files? Even if it is the same person there could be different spellings. The lastlogin info is not in the password files so where would you get that from using awk and from which of the 3 servers?
If you are more specific about your requirements, someone might be able to help you.
James R. Ferguson
Acclaimed Contributor

Re: Help in a script

Hi Wagar:

> I want to add the time and date when the user logged in last time.

From which server would you want the last login information, or don't you really want the most *recent* timestamp of all of them?

We offered considerable insight if you revisit your thread here:

http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1263622

Regards!

...JRF...
Waqar Razi
Regular Advisor

Re: Help in a script

Name and description from the first passwd file is fine and last login from the first server is fine using last
Court Campbell
Honored Contributor

Re: Help in a script

if the stems are trusted systems you could get the last successful login time with getprpw. You might be better off seeing if ldap keeps last login info in the db and just implementing it.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: Help in a script

Sorry. stems should be systems.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Dennis Handly
Acclaimed Contributor
Solution

Re: Help in a script

>Real name, description and if possible I want to add the time and date when the user logged in last time.

As JRF mentioned, look at your other thread.
If you want the name and description, you would need to capture more fields:
BEGIN { getline; name = $1; uid = $3; rname = $5 }
...
print name, uid, rname # print last
name = $1; uid = $3; rname = $5
...
END { print name, uid, rname } '
Waqar Razi
Regular Advisor

Re: Help in a script

Denis,

Thanks alot for your help, could you please help me a little bit more. Can you please little more explain working of your script so that I can modify it further?
Dennis Handly
Acclaimed Contributor

Re: Help in a script

>Can you please little more explain working of your script so that I can modify it further?

From the one in:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1263622

sort passwd1 passwd2 passwd3 | awk -F: '
function do_print() { # centralzed
save_line = name " " uid " " rname # add uid and real name
# get the info from last
last_command = "last -1 " name
name = $1; uid = $3; rname = $5
FS=" "
xx = last_command | getline
if (xx == 1)
print save_line, $3, $4, $5, $6
else
print save_line, "???"
FS=":" # restore
}
BEGIN { getline; name = $1; uid = $3; rname = $5 } # read first line
{
if ($1 == name) { # combine same UIDs
uid = uid " " $3
} else {
do_print() # if a break, print previous
}
}
END { do_print() } '
Waqar Razi
Regular Advisor

Re: Help in a script

Thank you very much, is that c shell scripting ?
James R. Ferguson
Acclaimed Contributor

Re: Help in a script

hI:

> is that c shell scripting ?

No, Dennis used 'awk'. This is invoked with the command 'awk'. The actual script is encapsulated between the single quote marks. A rather nice tutorial can be found here, although the tutorial is the enhanced GNU 'awk' which adds features not present in HP's more standard version.

http://www.gnu.org/software/gawk/manual/

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Help in a script

>is that c shell scripting?

I wouldn't be caught dead using the scummy C shell. ;-)

You can also put the awk script in a file and invoke with: awk -f foo.awk