SKYCUBE.net

Solutions for Go, MySQL, PHP, Linux and more

Ping http address via linux console

Posted — Jun 3, 2015

Ping a HTTP(s) address with a little tweak to the .bashrc or .profile file in Linux (Debian, Ubuntu, CentOS etc) with a web address copied from the browser location bar.

Have you recently (within the last year(s)) come across the issue if you copy the domain name/url from the browser window i.e. Firefox and instead of copying just your selection it will also copy the protocol (http or https).

For example if you want to ping this website and copy the domain name, your clipboard will end up with “https://skycube.net”.

If you then paste it into the console you end up like this:

linux:~# ping http://www.skycube.net
ping: unknown host http://www.skycube.net

To solve this issue you can tweak the .bashrc of .profile in your home directory on your machine:

Open in your preferred editor the .bashrc file

linux:~# vi ~/.bashrc

Somewhere around the top paste the following lines:

function ping() {
   pinghost=`echo $1`;
   pinghost=`echo $pinghost | sed 's/https:\/\///g'`;
   pinghost=`echo $pinghost | sed 's/http:/\/\//g'`;
   pinghost=`echo $pinghost | sed 's/\///g'`;
   /bin/ping $pinghost $2 $3 $4
}
export -f ping

Now open a new terminal and try your luck (note that the .bashrc and .profile file will be loaded only once you open a new console or type ‘bash’ into an existing terminal)

Lets enter the ping command from above again:

linux:~# ping http://www.skycube.net/
PING www.skycube.net (123.123.123.123) 56(84) bytes of data.
64 bytes from skycube.net (123.123.123.123): icmp_seq=1 ttl=51 time=38 ms
...

Maybe there are different ways or better methods, but the above works fine for me