The Use Case

So I am working on CS50's Problem Set 6: Web Server where I am asked to implement my own web server in C. I get to a point in the instructions where I need to enter commands into command line program telnet to test that my web server is returning what I expect. I can't easily repeat the HTTP request because the commands are not being entered in Bash but in the telnet program itself.

My first thought is, how can I automate this to make it easier to repeat these commands test by web server out, like a beast? Well, a few quick Google searches later and I found a great solution to my problem on Stackoverflow.

The Solution

The first step is to create a file named repeat.sh. This is the file that is going to store the commands in it that we are wanting to repeat. Save that file somewhere on your computer. I stored it on the desktop for this example. You will also want to make repeat.sh executable.

I am running the the staff's web server locally on 192.168.56.101:8080.

Inside repeat.sh put the following, changing the appropriate values based upon the HTTP commands you want to send to your host, etc.

echo "open 192.168.56.101 8080" 
sleep 2 
echo "GET /cat.html HTTP/1.1" 
echo "Host: 192.168.56.101" 
echo 
echo 
sleep 2

As you can see we are basically running a bash script that is printing the commands we want, intead of us having to type them manually.

Now to get this party started, simply run the following command in your terminal.

./repeat.sh | telnet

This will execute the bash script piping the output of the script into the telnet program. You should then see the HTML output that you would normally expect to see when working with telnet.

Now you can just rerun that command by hitting the up key on your keyboard and pressing enter! So much faster than typing all the HTTP command manually into telnet.

Note that I was using an Ubuntu 14.04 variant for this.

As usual if you come accross this and know of a better way or something I am doing completely wrong, then please reach out to me and help me improve! :)


Comments

comments powered by Disqus