Telnet Connection Test Script on Linux

For documentation purpose. The idea got from this thread forum. I adjusted it for my needs.

What I need is to test telnet connection from server X to server Y several times, and count the percentage of connected telnet success rate.

I create two script. Sorry I don’t have time to make the script cleaner.

  • The first one is the shell script that calls expect script. The shell script mainly the script that do the iteration and set the base IP and port. You also can manipulate to scan telnet connection from a sequence IP in some segment in the iteration.
  • The second one is the expect script. You can do much with expect. But I mainly just want to spawn the telnet here. You actually can decide whatever result of the telnet in the expect.

The bash script (

for (( i=1; i<=100; i++ ))
echo $i;
echo ‘\n’;
./monkey_bars.exp $newip $port

The expect script (monkey_bars.exp)

set timeout 5
set Machine [lindex $argv 0]
set Port [lindex $argv 1]

spawn -noecho -console telnet $Machine $Port
set log [open monkey.bars a]

expect {
login {puts $log "$Machine $Port UP!";}
-re "Connection refused" {puts $log "$Machine $Port DOWN";}
exit 1

There will be 4 steps :

1. Adjust the Script for your use. I set the total iteration, IP, and port in shell script. I set the timeout and the output filter in expect script.

2. Make sure you set the mod for those script to can be executed. mod 7. You also need to make sure that you can run bash shell script and expect script. If you can’t try su into root.

3. Execute the script, and in my case I stream the output into file.

./ > result.txt

4. Count the success from the file

grep -c "Connected" result.txt

If the result is 100, then all the telnet attempt to that IP and port is 100% successfull. (100 times success from 100 trials)

Hope this article helpful.


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:


You are commenting using your account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s