Auto exit Telnet command back to prompt without human intervention ^] quit close exit code 1
A slight improvement to the answer provided by Matthew above which allows you to use it inside shell scripts:
$ echo -e '\x1dclose\x0d' | telnet google.com 80
Connected to google.com.
Escape character is '^]'.
telnet> close
Connection closed.
$ echo $?
0
Automating telnet session using bash scripts
Write an expect
script.
Here is an example:
#!/usr/bin/expect
#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name
#The script expects login
expect "login:"
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact
To run:
./myscript.expect name user password
how do I sent escape character ^] through a spawned telnet session?
You send the ESC control sequence. Try send "\x1b\r"
can we exit without error from telnet with MozRepl?
You need to send ^]
character, that is an unprintable group separator character before your telnet client terminates your connection after executing all commands you gave it via pipe. Most versions of echo
program can produce unprintable characters using -e
option. Group separator is 035 in octal (you can see the entire ASCII table with man 7 ascii
on *nix systems.). So, the entire command should look like this:
$ (echo "content.location.href = 'http://v4.ident.me/'"; sleep 2; echo -e '\035'; sleep 2) | telnet localhost 4242 > /dev/null
$ echo $?
$ 0
Linux script to parse telnet message and exit
You could use internal TCP mechanism:
#!/bin/bash
exec 3<>/dev/tcp/127.0.0.1/80
# echo -en "eventualy send something to the server\n" >&3
RESPONSE="`cat <&3`"
echo "Response is: $RESPONSE"
Or you could use nc (netcat), but please don't use telnet!
How to connect to a phoneix framework app locally by telnet?
Phoenix uses Cowboy as underlying webserver. It has different timeout options, but the one we need is request_timeout
.
It defaults to 5_000
(in milliseconds) and can be changed inside configuration like this:
config :my_app, MyApp.Endpoint,
http: [
port: ...,
...
protocol_options: [
request_timeout: 60000 # minute here - for example
]
]
Now, you have a minute to type your:
GET /
inside telnet CLI
Related Topics
Prompting for User Input in Assembly Ci20 Seg Fault
Passing Main Script Variables into Perl Modules
Interprocess Communication Using Pipe in Linux
Executing String Sent from One Terminal in Another in Linux Pseudo-Terminal
How to Shell Have More Than One Job in Linux
How to Group by Column Values into Row and Column Header and Then Sum the Value
Find and Remove a Line in Multiple Files
Ansible Inside Script Command Not Found
How to Setup the Passwords-Less Authentication Between Two Different Accounts
Linux Script Extract Information from Excel to Create Users
How to Check That Two Folders Are the Same in Linux
How to Decrease the Size of Generated Binaries
Find File Then Cd to That Directory in Linux
Create Iptables Rule Per Process/Service
How to Write a Linux Daemon with .Net Core
How to Download a File from the Internet to My Linux Server with Bash