Telnet script

Hi all,

I would like to write a ad hoc shell script that would allow me to do some telnet tests to multiple IP's and ports at the same time . So, I want to determine which one is successful and which one fails. So, basically I would like the shell to spawn the telnet test and to break out of it when it fails ( Trying to connect for more than 5 sec ) and also break out of it when it succeeds and then move on to the next test.
Let's say for example I would like to run these tests:

telnet 172.27.26.3 2200
telnet 172.89.6.4 443
telnet 172.4.5.6 1200
telnet 172.2.3.4 229
telnet 172.4.3.5 25
telnet 123.5.6.7 443
telnet 229.4.5.6 80
telnet 72.8.7.5 53

It must be telnet?

If you can use ssh, you can try:

sshOptions="-oPort=22 -oPreferredAuthentications=publickey -oPubkeyAuthentication=yes -oKeepAlive=yes -oConnectionAttempts=1 -oNumberOfPasswordPrompts=1 -oStrictHostKeyChecking=no"
echo "Test" | ssh -T -C ${sshOptions} -l ${remoteUser} ${remoteHost} 1> /dev/null 2>&1

You have several ssh option: UNIX man pages : ssh (1)

For the user/password, not to ask it while executing the script, check: OpenSSH Public Key Authentication

Regards.

---------- Post updated at 15:28 ---------- Previous update was at 15:08 ----------

One comment I forgot is to test the ssh return code after execution:

echo "Test" | ssh -T -C ${sshOptions} -l ${remoteUser} ${remoteHost} 1> /dev/null 2>&1
sshReturnCode=$?

if [ ${sshReturnCode} -ne 0 ]
then
      echo "ERROR: Failed to access host:[ ${remoteHost}]."
fi

It will not be access to some machine. It will be telnet to some applications. Like telnet to see if I can reach www.google.com to see if I have good connectivity.

Ok!

We have two options:

With "expect":

#!/usr/local/bin/expect 
spawn telnet <machine ip> 
expect "login:" 
send "<username>\n" 
expect "Password:" 
send "<password>\n" 
send "bash\n" 
send "cd /opt\n" 
send "ls -ltr\n"
interact 

How to execute the �expect� command expect �f <file name>

expect �f <filename>.expect

If you are in a linux box, or in your Unix distribution you have: "autoexpect", it creates an "expect" script for you. Check this link: autoexpect

==========================
Or, there is a post here that shows a way to use telnet:

http://www.unix.com/shell-programming-scripting/11359-need-script-passwd-cant-use-expect-tool-post40791.html\#post40791

I hope it helps!

Regards.