How to Use the Command 'telnet' (with examples)
Telnet is a protocol that allows you to connect to remote computers over a network. It has been a fundamental part of network protocol standards for decades and allows users to execute commands on a remote machine. The most common use of the telnet command line tool is to diagnose and test connectivity issues on networks. Although it’s been largely replaced by more secure protocols like SSH, telnet still plays a valuable role in network diagnostics and legacy systems access.
Use case 1: Telnet to the Default Port of a Host
Code:
telnet host
Motivation:
Using the telnet command to connect to the default port of a host is useful for testing basic network connectivity and ensuring that the host is responding on its default telnet port, which is usually port 23. This is a fundamental test when you need to verify whether a host is up and if it’s accepting connections. For example, network administrators often use this method to check the availability of network devices or servers.
Explanation:
In this command, telnet
is the command line tool being used, and host
represents the hostname or IP address of the machine to which you want to connect. The default port used by telnet is port 23, which is generally employed unless another port is explicitly specified.
Example Output:
After executing this command, you’ll see a prompt asking for a username and password if the connection is successful. If the host is unreachable, you’ll receive a “Connection refused” message or “Unable to connect.”
Use case 2: Telnet to a Specific Port of a Host
Code:
telnet ip_address port
Motivation:
Connecting to a specific port of a host using telnet is valuable for testing connectivity to services that operate on specific ports other than the default telnet port. For instance, if you want to test whether a web server is responding, you might connect to port 80 (HTTP) or port 443 (HTTPS). It helps in diagnosing service-specific issues, such as whether a firewall rule is inadvertently blocking access to a certain port.
Explanation:
Here, telnet
is the command, ip_address
refers to the IP address of the host you are trying to reach, and port
is the specific port number you wish to connect to. This command allows for checking connectivity and access on non-standard telnet ports.
Example Output:
You might see a welcome message from the service running on the specified port if the connection is successful. If there’s an issue, you might see “Connection refused” or nothing at all if a network device is silently dropping packets.
Use case 3: Exit a Telnet Session
Code:
quit
Motivation:
Exiting a telnet session cleanly is important to ensure that all connections are properly closed and resources are freed. This prevents network resources from being tied up and ensures that you aren’t leaving a session open unintentionally, which could pose a security risk.
Explanation:
The command quit
is used within a telnet session to terminate the connection. This ends the session gracefully and returns you to your local machine’s command prompt.
Example Output:
When you type quit
and press enter, you will exit the telnet session, and the command prompt of your local machine will be displayed. There won’t be additional output from the telnet command itself.
Use case 4: Emit the Default Escape Character Combination for Terminating the Session
Code:
<Ctrl> + ]
Motivation:
Using the escape character combination in telnet is crucial for users to regain control of the current session, especially when a connection becomes unresponsive or you need to interact with the telnet command prompt without logging out entirely. This is vital for troubleshooting or if you want to send another command or change settings within the session without fully terminating it.
Explanation:
The <Ctrl> + ]
combination signals the telnet client to access the command mode. From there, you can issue a variety of commands to manage the session. It’s similar to other control sequences used in command-line applications to interrupt or provide an input to the program without closing it.
Example Output:
Executing <Ctrl> + ]
during a telnet session will bring you to the telnet console with a telnet>
prompt, allowing you to enter commands such as quit
to close the session or others like send
to issue commands without breaking the connection entirely.
Use case 5: Start telnet
with “x” as the Session Termination Character
Code:
telnet -e x ip_address port
Motivation:
Setting a custom escape character when starting a telnet session is useful in scenarios where the default escape sequence interferes with the activity intended for the session. This might be necessary when working with applications that use the same sequence or in environments with unusual keyboard layouts. By setting your own character, you maintain control over session termination.
Explanation:
In this command, telnet
calls the program, -e x
sets the character x
as the escape character, replacing the default <Ctrl> + ]
, ip_address
is the address of the host, and port
is the port you wish to connect to. This allows users to personalize their telnet session controls.
Example Output:
Upon pressing x
during your session, you will be brought to the telnet command prompt (telnet>
), where you can enter management commands. If you enter x
, the system acknowledges the new escape character you’ve set.
Use case 6: Telnet to Star Wars Animation
Code:
telnet towel.blinkenlights.nl
Motivation:
This is a fun, albeit non-functional, use of the telnet command, which serves to demonstrate its ability to display ASCII-based network services. Connecting to towel.blinkenlights.nl
using telnet streams an ASCII art version of Star Wars: A New Hope. It’s an entertaining demonstration of telnet’s capability to handle text and stream content over the network.
Explanation:
In this command, telnet
is the application, and towel.blinkenlights.nl
is the host to which you wish to connect. There is no port specified, as it defaults to the default telnet port 23. This facilitates engagement with the quirky ASCII art project running continuously at this host.
Example Output:
Once connected, you’ll be presented with an animated version of Star Wars: A New Hope in ASCII art, scrolling through the terminal. It’s a clever use of telnet to present a bit of fun content across the network.
Conclusion
Telnet, while somewhat antiquated, remains a vital tool in the network administrator’s toolkit due to its simplicity and ease of use in diagnosing network issues. Understanding how to effectively employ various telnet command options allows users to troubleshoot connectivity problems, test network services, and, occasionally, provide some whimsical entertainment with interactive ASCII art demonstrations. Despite modern alternatives, telnet continues to serve niche purposes across the tech landscape.