Telnet error- Escape character is '^]' and connection closed by foreign host

In program, I want to telnet the ports present in telnet.txt file and capture screenshot of open port which is opened on new terminal.

Problems:

  1. Escape character is '^]' and Connection closed by foreign host.
  2. Loop is not working properly. It only take one ports and closed the connection.

My program:

readfile()
{
while read -r line
do
name="$line"
echo "$name"
count=$line
xterm -e telnet $line
telnet $line
((count++))
gnome-screenshot xterm -e telnet $line
done < telnet.txt
}
readfile

telnet.txt

192.168.1.95     1027
192.168.1.95     1028
192.168.1.95     1030
192.168.1.95     2869
192.168.1.95     5357
192.168.1.95     10243
192.168.1.95     135
192.168.1.95     139
192.168.1.95     445
192.168.1.95     554
192.168.1.95     902
192.168.1.95     912
192.168.1.95     1025
192.168.1.95     1026

Output:

192.168.1.95     1027
Trying 192.168.1.95...
Connected to 192.168.1.95.
Escape character is '^]'.
Connection closed by foreign host.

Hmm.
Use a here document.
You can change the escape character in telnet - example set to capital letter F
This sets the escape character, connects to some remote node, then exits.

telnet  << !
set escape F
open $line
F
close
!

Plus, you seem to be making several connections to the same host.

xterm -e telnet $line
telnet $line
.... 
gnome-screenshot xterm -e telnet $line

Still it gives below error. Please help.

Program-

readfile()
{
while read -r line
do
name="$line"
echo "$name"
count=$line
telnet  << !
set escape F
open $line
xterm -e telnet $line
telnet $line
((count++))
F
close
!
gnome-screenshot xterm -e telnet $line
done < telnet.txt
}
readfile

Output-

192.168.1.95     1027
telnet>Escape character is 'F'.
telnet>Trying 192.168.1.95...
Connected to 192.168.1.95.
Escape character is 'F'.
Connection closed by foreign host.
telnet> escape character is 'F'.
telnet> (to) usage: open [-l user] [-a] host-name [port]
telnet> telnet> ?Invalid command
telnet> ?Invalid command
telnet> telnet> ?Invalid command
telnet> Need to be connected first for `bye'.

Please help me if any one has idea about telnet script.

Are you able you connect to the remote host manually in a telnet session? You may want to introduce some split second delays between the telnet commands in your script.

To be honest i don't get the point of some parts of your script. The three lines marked bold make absolutely no sense at all and should be removed from the here-document:

readfile()
{
while read -r line
do
name="$line"
echo "$name"
count=$line
telnet  << !
set escape F
open $line
xterm -e telnet $line
telnet $line
((count++))
F
close
!
gnome-screenshot xterm -e telnet $line
done < telnet.txt
}
readfile

Understand that, whatever you put between "<< !" and "!" goes as input/command to your telnet session. Ask yourself if you want to give "xterm -e telnet $line" as a command to your telnet session - most probably not, i suppose. The same goes for "((count++))": what should telnet make of that?

I hope this helps.

bakunin

1 Like

bakunin makes some great points. I did not understand your script either.
You appear to do things that require a shell command line rather than something telnet does.

Instead of telling us how you want to do something, pretend you do not know how to do it. Give us some requirements. Then we can work with you to get what you need.

Example:
I need to graph some data that lives on another machine. But I need to have it run on my desktop machine.

  1. I have create telnet.txt file by using nmap scan. File contain IP address with open port.
    e.g
    telnet.txt
ip          open port  
192.168.0.1          21
192.168.0.1          21
192.168.0.101       25
  1. Telnet to these IP with open ports from telnet.txt file. If telnet confirmed that port is open then it should display new terminal with blank screen for open port. Take screenshot of every blank screen (that means open port) and save it automatically with name: ip address_port. e.g 192.168.0.1_21

I have attached screenshot of open port. I want screenshot of open ports like this

The description of what you want is straightforward enough, but i question the "legitimity" of the approach.

You create the list with open(able) sockets (an IP-port combination is actually called that) - so far, so good. I just don't understand, what you want with the telnet client in this scenario? I used telnet for a similar purpose regularly (i.e. to test for a certain printer operating by trying to telnet to port 515), but that was only in absence of some specialized tools - like nmap , which you used to create the list in first place. What exactly should be the gain in using telnet after you have already used nmap to establish a certain fact (the openable port)?

And what is this "screenshot" for? Wouldn't some kind of list output:

IP-1    port-1    OK
IP-2    port-2    NOT_OK
IP-3    port-3    OK
...

(for whatever "OK" would mean) be better than a long list of pictures (showing nothing, btw.)?

I hope this helps.

bakunin

In many case nmap gives wrong output. It shows open ports but while doing telnet to specific port it becomes close. To more accuracy i am doing telnet.
For evidence, I want screenshot of blank screen which is already attached above.
so if possible please help me to correct above code.