Skip to content

Is it up? ping and curl

IntermediateLesson 30 of 667 min

Check a machine answers with ping -c, fetch a page with curl, save it with -o, and read status codes and redirects with curl -I, on a pretend network.

The machines in these lessons are pretend. Your laptop, a web server called web, a database server called db, the router and the website example.com all live inside this page, and nothing you type leaves your browser. The commands and what they print are the real ones.

ping: does it answer?

ping sends a tiny message to another machine and times the reply. It keeps going until you press Ctrl+C, so -c 3 (count) is handy:

ping -c 3 web

Each reply line has a time: a few milliseconds is normal on a home network. The summary says how many came back. "Name or service not known" means the name itself is unknown; no replies at all means the machine is down, or something blocks ping.

curl: what does the website say?

A machine can answer ping while its website is broken, so to check a website, ask for a page. curl fetches a URL and prints what comes back:

curl http://web/

curl -I shows only the headers, starting with the status:

StatusMeans
200 OKit worked
301, 302moved: the Location header says where
404 Not Foundno page at that address
500, 502the server, or the app behind it, failed
curl -I http://web/nope

-o FILE saves the page instead of printing it, and -L follows redirects to the final page.

Your turn

Check that web answers with ping -c 3. Save the shop’s home page, http://web/, as shop.html with curl. Then find out where http://www.example.com/ redirects to, from its headers.

Next