System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

loop through the accounts and servers

SOLVED
Go to solution
pareshan
Regular Advisor

loop through the accounts and servers

Hi everyone, I am trying to use loop (for loop but can be any loop) which will read from the file (text file) which will have 2 column one for account and another for server which kind of look like this

account1 server1
account2 server2
account3 server1
account4 server1
5 server3
6 server2
7 server2
8 server3
9 server4
10 server4

now here I have to run couple of ksh scripts which i wrote (example tar, stop, start). I have to read from the text file and using those accounts and respective servers I have to login and run those scripts one at a time for all the accounts and respective servers.

If there are 10 accounts they may be in 3 or 4 differnt servers but thats not a big issue I can put that in some sequence if needed. Write now I am doing it using multiple text files. If I have accounts in 5 servers then I put five text files where I put accounts name and loop through them using resepctive server but I need some way to do that using one text file as I have written before.

I hope I made it clear this time if not i will try to elaborate more later on

I will really appreciate any help
thanks alot
19 REPLIES
Steven E. Protter
Exalted Contributor

Re: loop through the accounts and servers

Shalom,

Can I see the code you are using.

I'd suggest two loops and two files, one for server the second for accounts within the server.

But to see where your code is going wrong, I kind of need to see it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: loop through the accounts and servers

Hi:

Using 'remsh' or better yet, 'ssh' you could do something like:

#!/usr/bin/sh
while read ACCT HOST X do
ssh -n ${ACCT}@${HOST} 'uname -a'
done < accts_and_servers

...where the 'accts_and_servers' file looks like your two-column file as posted. Substitute whatever script or commands you need in place of 'uname -a' in the sample script.

Regards!

...JRF...
pareshan
Regular Advisor

Re: loop through the accounts and servers

Hi james, could you make me more clear plz
I am little bit confused may be I dint understand all the code

thank you

Steven,

Till now Im just doing for one server
for account in `cat list`
do
run scripts
done < list1

this works fine for the current server coz we dont have to login and all coz its local system but i want to do the same for the accounts in other than the local server and loop through it so that for example
if act1 is in server5 it should connect first using that account name and the server name and run those scripts over there and go for another account from the list with the respective server

thanks alot
James R. Ferguson
Acclaimed Contributor

Re: loop through the accounts and servers

Hi (again):

> Hi james, could you make me more clear plz
I am little bit confused may be I dint understand all the code

The suggested code reads:

while read ACCT HOST X do
ssh -n ${ACCT}@${HOST} 'uname -a'
done < accts_and_servers

This reads a file called 'accts_and_servers' which consists of whitespace-delimited fields. The First field is an account and the second a server hostname. Any additional fields are ignored. The loop reads the file and runs a secure-shell command to each account/server pair. The command, in this example, is simply a request to return the server's 'uname' information.

Regards!

...JRF...
pareshan
Regular Advisor

Re: loop through the accounts and servers

James,
can i do that using rlogin instead of ssh because some of the servers and accounts are set such a way that I only login using rlogin if i do ssh it prompt for password and I cant make it automated using public key private key also.

like
rlogin server_name -l account_name 'script name'
If not how can i run command inside those server accounts using remsh will help?
thanks
Dennis Handly
Acclaimed Contributor

Re: loop through the accounts and servers

>can I do that using rlogin instead of ssh

You'll need to use remsh instead.
remsh server_name -l account_name 'script name'

For -l account_name to work, you would have to be in the ~account_name/.rhosts file.
Elmar P. Kolkman
Honored Contributor
Solution

Re: loop through the accounts and servers

First: don't forget the -n to remsh, otherwise it will only run the first line of your account/server list.
Second: I would strongly suggest to look into the ssh solution... It's more robust then remsh and a lot more secure...

If you take SEP's solution and replace the ssh command to:
cat accounts_and_servers | while read ACCOUNT HOST X
do
remsh $HOST -l $ACCOUNT -n "uname -a"
done

It will work this way.
If you have a script you want to distribute and run, change the remsh to these 3 lines:
rcp script $ACCOUNT@$HOST:/tmp
remsh $HOST -l $ACCOUNT -n /tmp/script
remsh $HOST -l $ACCOUNT -n rm /tmp/script

You might even try:
cat script | remsh $HOST -l $ACCOUNT /bin/sh
Every problem has at least one solution. Only some solutions are harder to find.
pareshan
Regular Advisor

Re: loop through the accounts and servers

Though what im saying wont solve my problem completely but I have tried to use two loops here because in my list there are some of the account which resides in the local server so I dont have to remsh for that. So i tried to match those accounts and run locally and for rest of the accounts do remsh but even if the account is not in the list
its doing the same thing its supposed to go inside else statement but its not.
this is my code.

while read Account Host X

do
if [ ${Account}="account1" || "account5" ]; then
${script} -a script -p "${WORK_PATH}stop" -c specific -l ${Account}
${script} -a script -p "${WORK_PATH}start" -c specific -l ${Account}
else
remsh ${Host} -l ${Account} -n 'compile'
fi
done < list1

I am not able to find the problem here. help plz
thanks
James R. Ferguson
Acclaimed Contributor

Re: loop through the accounts and servers

Hi:

Change the 'if' to:

if [ ${Account} = "account1" -o ${Account} = "account5" ]; then

Regards!

...JRF...
pareshan
Regular Advisor

Re: loop through the accounts and servers

its not working but

if [[ ${Account} = "tuxbug" ]] || [[ ${Account} = "tuxapi" ]]

is working thanks anyway
Dennis Handly
Acclaimed Contributor

Re: loop through the accounts and servers

>I have tried to use two loops here because in my list there are some of the account which resides in the local server so I don't have to remsh for that.

Why special case the current machine? You can remsh to it.
pareshan
Regular Advisor

Re: loop through the accounts and servers

Im saying for example if i have
list1
act1 server1
act2 server1
act3 server3
act4 server3
act5 server4

if im running the script from the server1 then can i use remsh here for act1 and act2? I thought I cant thats why first I tried to use if to select the accounts that are in the server1 and for rest of the account use remsh.

Im doing gud? I know this way its working but If I can run the script on local or remote server using remsh then I dont need to use that extra if statement to check the accounts in current server.

Because remsh will have

remsh servername -l actname -n scripts

if i run this from the local server it will be like im trying to connect server 1 from server 1. thats what I thought. Is there any other way ?

thanks

James R. Ferguson
Acclaimed Contributor

Re: loop through the accounts and servers

Hi:

> its not working but ... is working thanks anyway

How is what I suggested not working based on what you have asked? I think you are making this more difficult than it is.

As noted, if you don't have or won't configure and use 'ssh' use 'remsh'. As noted, you can 'remsh' to yourself the server on which the process is running). It's an easy simplification of logic and enables your script to be initiated on any of your servers and 'remsh' or 'ssh' to the remaining ones.

Regards!

...JRF...
...JRF...
pareshan
Regular Advisor

Re: loop through the accounts and servers

I was saying

if [ ${Account} = "account1" -o ${Account} = "account5" ]; then

is not working and

if [[ ${Account} = "tuxbug" ]] || [[ ${Account} = "tuxapi" ]]
this is working

About Remsh, I tried to run remsh from
server1 to server1 to execute some commands and it says login incorrect.

remsh server1 -l user1 -n ~/scripts/stop
remshd: Login incorrect.
Dennis Handly
Acclaimed Contributor

Re: loop through the accounts and servers

>if i run this from the local server it will be like i'm trying to connect server1 from server1. that's what I thought.

Yes, that's what it does. It works, why try to optimize it?

>JRF: enables your script to be initiated on any of your servers and 'remsh' or 'ssh' to the remaining ones.

Of course you could compare to $(hostname) so the script doesn't hardcode accounts or servers. :-)
James R. Ferguson
Acclaimed Contributor

Re: loop through the accounts and servers

Hi:

> Dennis: Of course you could compare to $(hostname) so the script doesn't hardcode accounts or servers.

Obviously that is another option.

...JRF...
pareshan
Regular Advisor

Re: loop through the accounts and servers

Everything else is fine and working. Just I have one query.

Is there any way we can do that by using
rlogin but should work similar way as remsh or ssh. I know it will prompot for password but I was thinking if we can pass some file from command line as a parameter or something like that whatever works.

Just like in ftp that we can save user name and password in .netrc file in home directory. and just

ftp ftp.yourdomain.com

will work

thanks
OldSchool
Honored Contributor

Re: loop through the accounts and servers

"Is there any way we can do that by using
rlogin but should work similar way as remsh or ssh"

ssh was suggested as its' more secure, and w/ the keys properly setup, its not going to require a password.

rlogin is just that, a login session on a remote host.

remsh will do what you need, as you specified it. I take it you now have a problem with that solution as well????
Dennis Handly
Acclaimed Contributor

Re: loop through the accounts and servers

>Is there any way we can do that by using rlogin but should work similar way as remsh or ssh?

Why? The purpose of rlogin is to login and for remsh to execute a command.

>Just like in ftp that we can save user name and password in .netrc file in home directory

Both r* commands use ~/.rhosts so you don't need a password.