login to multiple server

i want to login (telnet) to 2 different servers. Then I will execute command in those servers as many time as I need(i will choose the server to execute the command). Is it possible?

i.e, my script will be stayed log on two servers (server1, server2). Then I will execute command by choosing one server at a time. It will take input from the terminal and decide from which the command has to be executed. I wanna to avoid login every time I need to execute command.

it may be bash, perl or others. i can log on using expect script. please help me

I would suggest using passwordless SSH instead of telnet(1), you can then run:

# ssh user@server.domain.com command

Do you can do something like:

COMMAND="ls -l"
SERVERNAME=server.domain.com
ssh user@${SERVERNAME} "${COMMAND}"

And use a case statement to select the server depending on what command you have selected if the command is to be a parameter to your script then that line needs to be:

COMMAND="$@"
if [ -z "${COMMAND}" ]; then
  echo "ERROR: No command given as parameter, aborting!"
  exit 99
fi

.

Either that or look into using /etc/hosts.equiv on the server but telnet and hosts.equiv are insecure.

Does this help?

Thanks TonyFullerMalv for ur quick reply!

passwordless SSH is not possible for me as I'm not root user of the server. Is it possible by using subshell?

you can be any user to use passwordless ssh .So you can proceed with TonyFullerMalv's suggestion.

Thanks TonyFullerMalv
I can use either ssh or telnet. It is not the problem. I do not want to log on every time I execute the command. Main thing is that I will be stayed log on to two servers from a single script and It will execute my desired command to desired server taking input of the shell.
Is it possible by using subshell? i'm not clear about subshell.