I am unclear why you wish to create three output files but since it is your requirement and not mine, perhaps the following will help.
while read host port; do
preamble="telnet $host $port"
case $(telnet $host $port </dev/null 2>&1 | tail -1) in
(*closed*) echo "$preamble ... Connected" >>Telnet_Success.txt
(*refused*) echo "$preamble ... Refused" >>Telnet_Refused.txt
(*) echo "$preamble ... Failed" >>Telnet_Failure.txt
The first line allows you to decide to give the input data in another file (e.g. data.txt) by providing that as an argument to the script. For example
$ telnet.sh data.txt
If this is not given it will assume the file 'ip.txt'.
Then use a case statement to match the last line of the command output to determine the file in which the result message is placed.
Note that the
read statement allows you to pick up separate "words" on the input line -- thus removing the need for the use of commands like
cut here. Also the way you have used your files means that succeeding runs of the script are added to existing output lines. That may not be what you want.
P.S. Strictly speaking I should have placed a shebang line at the top as in:
or similar. I relied here on the fact that I am running on a Linux system with a Bash shell.