Web services on the command line

I spend a lot of my day in a terminal window, and I love automating stuff with bash scripts. Sometimes those scripts need to do perform actions based on external conditions: location (country), weather, bandwidth speed …

Here are a couple of terminal-ready web services that can give you external information. You can interrogate them from the command-line (CLI). For this you will need a CLI web client like curl, wget or http; at least one of those should already be installed on your MacOS/Linux/Win10 WSL terminal.

IP address/location

There is the website ifconfig.co, run by Martin Polden (Norway), based on his mpolden/echoip project (written in Go). It will give you your IP4/IP6 network address, and based on that, also your approximate geographical location.

  • curl ifconfig.co : 2a02:1811:142c:6000:e968:27b8:17d6:10bd
  • curl ifconfig.co/city : “Vilvoorde” (your nearest city)
  • curl ifconfig.co/country : “Belgium” (your country or the one your IP address seems to indicate)
  • curl ifconfig.co/country-iso : “BE” (your country code)

Weather

Igor Chubin has developed a neat ASCII weather forecast service wttr.in that works great in CLI mode. It runs on Python and the code is on chubin/wttr.in.

  • curl wttr.in : get the weather for the location your computer is at
  • curl wttr.in/London : get the weather for London
  • curl "wttr.in/?format=4" ; “Vilvoorde, Belgium: ⛅️  🌡️+14°C 🌬️→31km/h”
  • curl "wttr.in/?format=%l+:+%d+%m" : “Vilvoorde, Belgium : 22:38:16 🌕” (sunset timing & moon phase)

Developer help

Igor also developed cheat.sh (github: chubin/cheat.sh) which allows a fast way to answer “how does XYZ work again?”

  • curl cht.sh/rsync : show usage for rsync
  • curl cht.sh/php/facade : what is a PHP facade for again?
  • curl cht.sh/go/Channels : how do Go channels work again?

Create QR code

Another genius project of Igor Chubin: qrenco.de (github)

Bandwidth speed

Well this is a bit different, because you can’t measure internet speed with just a call to an external service.

There is sindresorhus/fast-cli which uses the Fast.com (Netflix) servers, but it’s a huge npm library (with puppeteer, i.e. a full headless browser under the hood) you have to install on your local computer,

A bit lighter is sivel/speedtest-cli, which is a Python script that uses the servers from Speedtest. It is lighter in that it fetches the last version of the code from Github (with curl) and feeds this right into a python interpreter. Obviously you need Python on your machine.

$ curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
Retrieving speedtest.net configuration...
Testing from Telenet (94.225.186.144)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Proximus (Schaarbeek) [7.34 km]: 19.867 ms
Testing download speed...
Download: 128.29 Mbit/s
Testing upload speed...
Upload: 18.26 Mbit/

Dictionary lookup

$ curl dict://dict.org/d:albeit
150 1 definitions retrieved
151 "Albeit" gcide "The Collaborative International Dictionary of English v.0.48"
Albeit \Al`be"it\, conj. [OE. al be although it be, where al is
   our all. Cf. {Although}.]
   Even though; although; notwithstanding. --Chaucer.
   [1913 Webster]


         Albeit so masked, Madam, I love the truth. --Tennyson.
   [1913 Webster]

Create short URL

$ curl -F shorten=https://blog.forret.com https://ttm.sh 
https://ttm.sh/hv6

Publish a file (image)

 $ curl -F'file=@test.jpg' https://0x0.st
https://0x0.st/iOC_.jpg
$ curl --upload-file ./results.txt https://transfer.sh/results.txt https://transfer.sh/66nb8/results.txt

Exchange rate (crypto-currencies)

Another CLI service by Igor chubin : rate.sx. Unfortunately there seems to be no public free service for exchange rates for ‘normal’ currencies. All companies who have the information, want you to pay for it.

Scripting

If you want to use these web tools in a script, you can easily put their output into a bash variable:

city=$(curl -s ifconfig.co/city)
city=$(wget -q -O- ifconfig.co/city)
city=$(http ifconfig.co/city)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.