Operating System - HP-UX
1825764 Members
2103 Online
109687 Solutions
New Discussion

Help with user delete script

 
Mike_305
Super Advisor

Help with user delete script

Hello,

I need to remove bunch of user that has UID "0" on my systems.

I have filter out all the users with the UIO "0" and have the list, also I have the server list for ssh.

What I am trying to fo is write simple loop something like this. but it's not working.

#!/usr/bin/sh
export RMVLST=$(cat usr.lst)
for X in $(cat srv.lst)
do
ssh ${X} grep ^$RMVLSTa /etc/passwd.test
done

I get the ERROR saying "grep can't open rmod.

Appreciate your help.

Thanks in advance for the help.

MJ
If there is problem then don't think as problem, think as opportunity.
11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: Help with user delete script

You can remove evil cats with:
export RMVLST=$(< usr.lst)
for X in $(< srv.lst); do

>I get the ERROR saying "grep can't open rmod.
ssh ${X} grep ^$RMVLSTa /etc/passwd.test

This is basically tring to grep ALL of the tokens in usr.lst. grep will only take one token at a time, unless you use "-f file". Or another loop:

for user in $RMVLST; do
ssh ${X} grep ^$user /etc/passwd.test

(I'm not sure if that "a" is a typo?)

If you can send that file over to each system or have it NFS accessible, you could have "^name" on each line.

Or you can copy that passwd file back to your machine and process it there.
Mike_305
Super Advisor

Re: Help with user delete script

Hello,

Thanks for the quick reply.

Yes, it was typo.

I did the way you are suggesting and get the following result.

ERROR = ssh: grep: no address associated with name

export RMVLST=$(< usr.list)
export SRVLST=$(< srv.lst)
for user in $RMVLST
do
ssh $SRVLST grep ^$user /etc/passwd.test
done

Thanks in advance - MJ
If there is problem then don't think as problem, think as opportunity.
Tingli
Esteemed Contributor

Re: Help with user delete script

How about put "^$user".
Mel Burslan
Honored Contributor

Re: Help with user delete script

>ERROR = ssh: grep: no address associated with name

>export RMVLST=$(< usr.list)
>export SRVLST=$(< srv.lst)
>for user in $RMVLST
>do
>ssh $SRVLST grep ^$user /etc/passwd.test
>done

Again, biting more than you can chew.

with this command

>ssh $SRVLST grep ^$user /etc/passwd.test

you are assuming you will loop thru the server list one server at a time, whereas ssh thinks all of those servers are one single server and crying fould not finding this server.

what you need to do

for s in $SRVLST
do
for u in $RMVLST
do
ssh ${s} "grep ${u} /etc/passwd.tst"
done #for variable u
done #for variable s

hope this helps
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Help with user delete script

Hi:

The error "ssh: grep: no address associated with name"

...suggests that your variable ${X} is empty. you can see this if you did:

# ssh grep ^$RMVLST /etc/passwd

...where ${RMVLST} is defined.

For :

#!/usr/bin/sh
export RMVLST=$(< usr.list)
export SRVLST=$(< srv.lst)
for user in $RMVLST
do
ssh $SRVLST grep ^$user /etc/passwd.test
done

...as Dennis already noted, you need another loop to walk the list contained in ${SRVLST}.

#!/usr/bin/sh
RMVLST=$(< usr.list)
SRVLST=$(< srv.lst)
for user in ${RMVLST}
do
for host in ${SRVLST}
do
ssh -n ${host} grep ^${user} /etc/passwd.test
done
done

...Notice too that you do not need to 'export' your variables into your shell's environment.

Regards!

...JRF...
Mike_305
Super Advisor

Re: Help with user delete script

Hello,

Ok, I am half way to my mission. A little help from you GURU's can put me in little success ship. LOL

This is what I came up with. I can run locally but I need to run on the remote server using ssh. I defiantly copy the working files over to each server.

And here is the loop, the issue is ssh part is not working. Please help.

#!/usr/sbin/sh
#export SRVLST=$(< srv.lst)
set -A users $(< user.list)
echo ${users[*]}
for user in ${users[*]}
do
#ssh $(SRVLST) grep ^${user##/*} /etc/passwd.test
grep ^${user##/*} /etc/passwd.test
done

Thanks in advance - MJ
If there is problem then don't think as problem, think as opportunity.
Mike_305
Super Advisor

Re: Help with user delete script

Sorry I did not see JRF's response. I will try that and let you know.


Thanks,

MJ
If there is problem then don't think as problem, think as opportunity.
Mike_305
Super Advisor

Re: Help with user delete script

Hello All,

I tried your options and now I can ssh into the server no prblem but my grep is not listing the user from the password file and not even giving me error.

Should I just copy the script to local node and then ssh to each server and just run it?

#!/usr/bin/sh
RMVLST=$(< usr.lst)
SRVLST=$(< srv.lst)
for user in ${RMVLST}
do
for host in ${SRVLST}
do
ssh ${host} "grep ^${user} /etc/passwd"
done
done

Thanks for everyones help.

MJ
If there is problem then don't think as problem, think as opportunity.
James R. Ferguson
Acclaimed Contributor

Re: Help with user delete script

Hi (again):

> I tried your options and now I can ssh into the server no prblem but my grep is not listing the user from the password file and not even giving me error

Try isolating a test case at the command line:

# ssh -n somehost "grep ^root /etc/passwd"

Regards!

...JRF...
Mike_305
Super Advisor

Re: Help with user delete script

Hello James,

I tried from command line and it works.

ssh -n $SRVLST "grep ^root /etc/passwd"

ssh -n rp5470-prd "grep ^root /etc/passwd"

I think I need test this out by pushing the user.list file on to all my server and then run the loop.

Thanks for helping me out, appreciate that very much.

Any ideas are welcome.

JRF - Thanks again....

I will keep trying until I break it....LOL

Thanks,

MJ
If there is problem then don't think as problem, think as opportunity.
Dennis Handly
Acclaimed Contributor

Re: Help with user delete script

>I think I need test this out by pushing the user.list file on to all my server and then run the loop.

Yes, unless you want to login multiple times.
Or you can copy the password file back to your machine, then process it.

>set -A users $(< user.list)

You really aren't buying much by creating an array.