I’ve been needing to use an embedded system that had network support but lacked many of the (useful) utilities that we’ve gotten used to. I needed this device to talk to an HTTP endpoint (an array of relays accessible via Ethernet) that was configured with basic authentication. With curl
, this is a simple case of running:
curl username:password@10.0.3.20/outs.cgi?out0=0
curl
happily understand this syntax and correctly negotiates basic auth. Without curl
, this task gets a little more involved…
The layer down would probably be nc
, or netcat that can be used to send raw data over a network. Unfortunately, talking to a HTTP server means you’ve also got to send HTTP headers. Pair nc
with printf
to write the HTTP GET
request:
printf "GET /outs.cgi?out0=0 HTTP/1.1\r\n\r\n" | nc -v 10.0.3.20 80
Adding basic HTTP authentication is fairly straightforward. Basic authentication requires username:password
to be encoded using Base64. Unfortunately, the base64
app was unavailable on my embedded device, but was available on my desktop environment:
echo -n "username:password" | base64
The use of -n
in the echo is required as it prevents the new line character from being appended to the string, thus causing the wrong password to be produced. The output can then be copied into the previous string with the added basic authorisation added:
printf "GET /outs.cgi?out0=0 HTTP/1.1\r\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\r\n\r\n" | nc -v 10.0.3.20 80
At this point, I received a sensible answer back from the web server. An example of a bad answer would be, say, an incorrect password, which would appear as:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Protected"
Connection: close
401 Unauthorized: Password required
The things you do when you don’t have access to curl
…