Ralph using a computer.

Useful code snippets and commands for Linux.

Posted: July 22, 2013. At: 9:45 PM. This was 5 years ago. Post ID: 6014
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

Alternatives to the ping and traceroute commands on a Linux system

There are many alternatives to the ping and traceroute commands on a Linux system. The mtr command is one of them. This command will trace the route the network packets are taking to the target IP address.

bash 06:42:02 Mon Jul 22 [homer@deep-thought $ mtr

This code will ping a server and use tcp, udp, icmp, stream, and syn packets to do so. This is useful to see if a server is up, but they block icmp packets, this script will work regardless of this and still allow you to see if the server is up or not. Good if you are behind a proxy at a college or something like that. There is also a tcpping command for Linux that can work when you are behind a restrictive firewall. There is some information about this here: http://xmodulo.com/2013/01/how-to-install-tcpping-on-linux.html.

use warnings;
use strict;
use Net::Ping;
# code source: http://www.perlmonks.org/?node_id=943892
# More: http://stackoverflow.com/questions/3960595/how-can-i-ping-a-host-with-a-perl-one-liner-with-netping
#$| = 1;
print "Please type a host to check: -:\n";
my $host = <>; #Reading input from STDIN.
if (length($host) < 3) {
    print "You did not type a host!\n";
my @proto = ("tcp", "udp", "icmp", "stream", "syn");
foreach my $pro ( @proto ) {
    print "#-Protocol $pro \n";
    my $p = Net::Ping->new($pro);
    # Specify source interface of pings
    print "$host is ";
    print "NOT " unless $p->ping($host, 2);
    print "reachable.\n";

This is a good alternative for tracing the route taken from one host to another.

homer@deep-thought ~ % sudo tcptraceroute
[sudo] password for homer: 
traceroute to (, 30 hops max, 60 byte packets
 1  localhost ( &lt;syn,ack&gt;  0.031 ms  0.004 ms  0.005 ms

This is how to ping the TCP port of a remote host. This is using the netcat tool.

[email protected] ~ % nc -zvv yahoo.cn 80
Connection to yahoo.cn 80 port [tcp/http] succeeded!

The hping3 command on Linux Mint 15 is very useful for sending TCP packets to a remote host to test the configuration of the server.

Type this command to install this utility: sudo apt-get install hping3.

And this is the output you get when you are pinging the Yahoo website with hping3.

bash 09:32:38 Mon Jul 22 [homer@deep-thought $ sudo hping3 -S -p 80 yahoo.com
HPING yahoo.com (eth1 S set, 40 headers + 0 data bytes
len=46 ip= ttl=112 id=51335 sport=80 flags=SA seq=0 win=8192 rtt=208.4 ms
len=46 ip= ttl=110 id=53804 sport=80 flags=SA seq=1 win=8192 rtt=197.2 ms
len=46 ip= ttl=110 id=56362 sport=80 flags=SA seq=2 win=8192 rtt=211.5 ms
len=46 ip= ttl=110 id=58939 sport=80 flags=SA seq=3 win=8192 rtt=211.0 ms
len=46 ip= ttl=110 id=61474 sport=80 flags=SA seq=4 win=8192 rtt=197.5 ms
len=46 ip= ttl=112 id=64019 sport=80 flags=SA seq=5 win=8192 rtt=195.7 ms
len=46 ip= ttl=112 id=998 sport=80 flags=SA seq=6 win=8192 rtt=194.8 ms
len=46 ip= ttl=112 id=3556 sport=80 flags=SA seq=7 win=8192 rtt=210.2 ms
len=46 ip= ttl=112 id=6243 sport=80 flags=SA seq=8 win=8192 rtt=210.6 ms
len=46 ip= ttl=110 id=8839 sport=80 flags=SA seq=9 win=8192 rtt=197.2 ms
--- yahoo.com hping statistic ---
10 packets transmitted, 10 packets received, 0% packet loss
round-trip min/avg/max = 194.8/203.4/211.5 ms

No comments have been made. Use this form to start the conversation :)

Leave a Reply