Ralph using a computer.

Useful code snippets and commands for Linux.

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 8.8.8.8

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.

#!/usr/bin/perl

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";
    exit(0);
}

my @proto = ("tcp", "udp", "icmp", "stream", "syn");

foreach my $pro ( @proto ) {
    print "#-Protocol $pro \n";
    my $p = Net::Ping->new($pro);
    chomp($host);
    # Specify source interface of pings
    print "$host is ";
    print "NOT " unless $p->ping($host, 2);
    print "reachable.\n";
    $p->close();
}

exit(0);

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

homer@deep-thought ~ % sudo tcptraceroute 127.0.0.1
[sudo] password for homer: 
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
 1  localhost (127.0.0.1) <syn,ack>  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.

homer@deep-thought ~ % 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 206.190.36.45): S set, 40 headers + 0 data bytes
len=46 ip=206.190.36.45 ttl=112 id=51335 sport=80 flags=SA seq=0 win=8192 rtt=208.4 ms
len=46 ip=206.190.36.45 ttl=110 id=53804 sport=80 flags=SA seq=1 win=8192 rtt=197.2 ms
len=46 ip=206.190.36.45 ttl=110 id=56362 sport=80 flags=SA seq=2 win=8192 rtt=211.5 ms
len=46 ip=206.190.36.45 ttl=110 id=58939 sport=80 flags=SA seq=3 win=8192 rtt=211.0 ms
len=46 ip=206.190.36.45 ttl=110 id=61474 sport=80 flags=SA seq=4 win=8192 rtt=197.5 ms
len=46 ip=206.190.36.45 ttl=112 id=64019 sport=80 flags=SA seq=5 win=8192 rtt=195.7 ms
len=46 ip=206.190.36.45 ttl=112 id=998 sport=80 flags=SA seq=6 win=8192 rtt=194.8 ms
len=46 ip=206.190.36.45 ttl=112 id=3556 sport=80 flags=SA seq=7 win=8192 rtt=210.2 ms
len=46 ip=206.190.36.45 ttl=112 id=6243 sport=80 flags=SA seq=8 win=8192 rtt=210.6 ms
len=46 ip=206.190.36.45 ttl=110 id=8839 sport=80 flags=SA seq=9 win=8192 rtt=197.2 ms
^C
--- 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
Ralph using a computer.
Ralph using a computer.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *