Operating System - HP-UX
1748179 Members
3879 Online
108759 Solutions
New Discussion юеВ

Re: shell script doubt in for loop

 
SOLVED
Go to solution
senthil_kumar_1
Super Advisor

shell script doubt in for loop

Hi

I want to create a shell script to find that is particular user available in what are the servers.

we are lot of hp-ux server. some users are created in some servers and not created in some servers.


so from being one hp-ux server i want to see this.

rlogin is enabled in all the servers.

and in rhosts file of the all the servers has the entry for connecting server (from which i am going to search the users.)

8 REPLIES 8
OldSchool
Honored Contributor

Re: shell script doubt in for loop

ok...so that's what you want to do, but what's your question? Have you attempted to write anything yet?

if not, one way might be

read "username" from command line parm
File w/ list of "servers"
for servername in above list
remsh grep ^username: /etc/password

details and implementation are left as an exercise.

there are other ways, and remsh / rlogin isn't secure.
Doug O'Leary
Honored Contributor

Re: shell script doubt in for loop

Hey;

If nothing's been configured/started already, then don't start out using rlogin/remsh. With a little bit of pre-work, you can use secure shell/public key authentication to get the same results with greater security and flexibility.

Do a search in itrc for procedures on configuring ssh/pka. It's relatively simple.

Once that's done, you can then do something like the following:

#!/bin/ksh

User=$1
[[ ${#User} -eq 0 ]] && exit 1

for h in $(cat list-of-hosts-file)
do
l=$(ssh ${h} grep "^${User}:" /etc/passwd > 2>/dev/null)
printf "%-8s %s\n" ${h} "${l:=Not defined}"
done

Hope that helps.

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell script doubt in for loop

Hi:

Ok, here's one way.

#!/usr/bin/sh
typeset USER=$1
while read HOST X
do
RESULT=$(rsh ${HOST} -n grep ^${USER}: /etc/passwd)
if [ -z "${RESULT}" ]; then
echo "${USER} missing on ${HOST}"
else
echo "${USER} exists on ${HOST}"
fi
done < /tmp/hosts

...

The '/tmp/hosts' file contains a hostname (one per line).

Now go examine the manpages for 'rsh' and note the use of the '-n' option. Notice that while I used a simple 'grep' I anchor what I want to match to the beginning of a line with the caret (^) symbol and specify the ":" to limit the match at the end of the string I want to be significant.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: shell script doubt in for loop

Hi (again):

Oops! Replace "rsh" with 'remsh' in my script. "rsh" = Restricted Shell. "remsh" stands for "remote shell".

Regards!

...JRF...
senthil_kumar_1
Super Advisor

Re: shell script doubt in for loop

Acutally i am using folllwoing method to find the users for single server.

Ex:

I am entering the following command directly in command prompt.

>for i in 10.250.0.2
>do
>ssh $i "grep john /etc/passwd
>done

So the above command is working fine for singel system

But i have lot of system.

So I created a file called "servers" and mentioned all the servers' ip address in that file.

Then i mentioned that file like

>for i in /root/servers
>do
>ssh $i "grep john /etc/passwd
>done


How to solve this.


James R. Ferguson
Acclaimed Contributor

Re: shell script doubt in for loop

Hi (again):

> How to solve this.

If yuo are already using 'ssh' in lieu of 'remsh' then, very good.

Both Doug and I showed you how to use a file containing a list of servers. Read what has been posted.

Too, your 'grep' will match things you don't want to match! Again, _READ_ what I said about matching to the beginning of a line and only up to a colon (":")!

You already have the answer to your question.

Regards!

...JRF...
Suraj K Sankari
Honored Contributor

Re: shell script doubt in for loop

Hi,
>for i in /root/servers
>do
>ssh $i "grep john /etc/passwd
>done

you have to do like this way
for i in `cat /root/servers`
do
ssh $i grep john /etc/passwd
done

Suraj
Taifur
Respected Contributor

Re: shell script doubt in for loop

Hi

check below link for shell,

http://docs.hp.com/en/B2355-90046/ch14s03.html

if helps,pls assign pont.

Rgds//
Taifur