Computer scienceSystem administration and DevOpsCommand lineNetworking

Checking External Resource

5 minutes read

Introduction

Quite often, you have to connect to a remote server/host to send or receive data. You might want to send/receive data using either commercial software or a program you wrote yourself. However, sometimes this data transfer fails. Now it is your job to troubleshoot and figure out where exactly the problem lies: Is it a connection issue or a problem with your program? Usually, the connection is tested first to check if it is working. If there are no issues with it, we move on to checking our code/software. In this topic, we will learn two commands to check the connection with a host server: ping and telnet.

We will use the terms remote host, remote machine, and remote server interchangeably in this topic.

Ping

The ping command is a network utility tool for checking connectivity with another machine. It is used to test if the network device is reachable. The syntax of the ping command is as follows:

ping <hostname/IP address>

Let's take a look at an example of pinging Google's server. After executing the command we get the following results:

$ ping google.com
Pinging google.com [xxx.217.167.174] with 32 bytes of data:
Reply from xxx.217.167.174: bytes=32 time=10ms TTL=116
Reply from xxx.217.167.174: bytes=32 time=8ms TTL=116
Reply from xxx.217.167.174: bytes=32 time=9ms TTL=116
Reply from xxx.217.167.174: bytes=32 time=9ms TTL=116

Ping statistics for xxx.217.167.174:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 8ms, Maximum = 10ms, Average = 9ms

In this topic we show you the output of commands on Windows OS. The syntax is the same for other operating systems but there might be a slightly different behavior or output.

The ping command works by sending an ICMP Echo Request Packet to the hostname/IP address given in the command. You can think of this packet as our computer asking the server "Hi! Are you alive?" If the server receives the packet, it sends back an ICMP Echo Reply Packet which is like a packet with a "Yes, I'm alive" response. If the reply packet is successfully received, then there is a working connection between our computer and the server. If the reply packet is not received, then the problem isn't with the code but with its connection to the server. You can also read a summary of the most common ping errors.

sсheme of the ping command's work

You can use various flags with the ping command. Below you can find a list of some of the most used flags:

list of the flags most used with the ping command

You can find the complete list of all the flags available with the ping command in the manual for ping. Use the man ping command on Unix systems or ping /? on Windows OS.

Telnet

Another command you can use to check connectivity with a remote host is the telnet command.

Telnet (Teletype Network Protocol) is an application protocol to connect with a remote host and communicate with it through simple text commands via the telnet command-line utility. Using telnet, you can do things like creating/deleting folders and files on the remote host even if the host is thousands of kilometers away.

Telnet was developed in 1969 in the early days of networking when there was no internet and no major concern for security against hacking. Consequently, telnet does not have an encryption mechanism. Messages in packets are sent across in plain text form which makes them vulnerable to sniffing by hackers. Hence, it is not recommended to use telnet on public networks like the Internet. You should only use telnet on a private LAN where you trust all the devices. For connecting with a remote host over the Internet, it is recommended to use the SSH protocol which has built-in encryption.

Telnet command

Let's have a look at the syntax of the telnet command:

telnet <hostname/IP address> <port number>

If you don't specify the port number, it defaults to port 23. You might be asked to enter username and password if the host is password-protected.

$ telnet www.example.com 80

A successful connection will open a blank Terminal screen like this one:

blank Terminal screen

If an error message like "Connection Refused" appears when you execute the command, it could indicate either one of the following:

  1. A firewall is blocking incoming connections on the host.
  2. A firewall is blocking outgoing requests on your computer.
  3. The remote host is down.
  4. Network connectivity between your computer and the remote host is unavailable for some reason.

Conclusion

In this topic, we've learned how to use the ping and telnet command-line utilities to test connectivity with a remote host. You can use either of them for this purpose. There are other commands too which can be used to check connectivity, but we will get to them later. Telnet can also be used to login into a remote host and send commands to it, but remember not to use Telnet to connect with a remote host on the Internet. Now, head over to the exercises to get some practice!

113 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo