I would like to send some text to a port on my machine using telnet
in the Mac OS X Terminal.
I start telnet
using:
telnet localhost 8080
Then I type some text. Is that text send automatically or do I have to do something to send the text?
I would like to send some text to a port on my machine using telnet
in the Mac OS X Terminal.
I start telnet
using:
telnet localhost 8080
Then I type some text. Is that text send automatically or do I have to do something to send the text?
Here's a simple Python server:
#!/usr/bin/env python
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 10 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
conn.send(data) # echo
conn.close()
Save it as server.py, and run it python server.py
.
Then try to connect using the terminal
telnet 127.0.0.1 5005
Then just type anything and press return
The server print the data in the console and send it back to you.
That way, you'll know what you have to do to send data via telnet.
When you typing the text in telnet
, by default it operates in Linemode which send the packets per line, so you just need to press Enter to send the command to the remote host (since terminal character processing on the client side). This is in order to reduce network traffic and it is very useful for long delay networks while typing the command line. If you need to send packets per character typed, then you need to switch to binary mode.
To test Telnet in Linemode, you can run the dummy server to be listening on the local port, e.g. by using netcat
(install if needed):
nc -vl localhost 1234
then in another terminal connect to your server by:
telnet localhost 1234
and start entering some text to check when the data is received.
Hit Control-] (^]
) and type quit
to finish.
It is also possible to send data using Bash shell by the following command:
cat > /dev/tcp/127.0.0.1/1234
then start entering text. When finished, hit Control-D.
The answer depends on what the remote end supports:
What the remote end does with the received input, depends on the remote end. Most server processes (HTTP, SMTP, POP, IMAP) tend to wait for end-of-line (EOL, newline) char before processing input. Some others may wait for end-of-file (EOF, ^D) before starting to process the input.
After connecting using telnet hit the escape character (usually CTRL + ]). If you do not know your telnet escape key type telnet
without parameters and it will display it, from there you can go back or use open [ip] [port]
,
Once connected, you hit the escape key (CTRL + ])which will send you back to telnet where you can then type
send [message]
If you'd like to get back to sending every key, just hit a random key that isn't a part of telnet's command list.
P.S new versions will send one line whilst just typing instead of every key and the send command can only be used for preset telnet commands unless you're on windows in which case you can send strings via send as well.
Try using send
command after you've opened connection to the destination host.
send "some text"
For more information about send
see man telnet
send
command is for sending special character sequences to remote machines, not random text. See the man page: developer.apple.com/library/mac/documentation/Darwin/Reference/…