Posts tagged 'console'

Notes on shell scripting

published on December 29, 2011.

Yesterday I did some shell scripting and thought about writing down the few things learned along the way. Amazing how little needs to be done to learn a lot :)

Result of a command to a variable

First thing I learned is how to “save” the result of a shell command to a local variable:

PHP_BINPATH=$(which php)

By enclosing the command in parenthesis and putting a dollar sign in front of it, will put the result of that command in the variable.

An empty variable

Turns out, a variable can be empty, null. Nothing strange with that, until one tries to do something with that variable. For example:

if [ $PHP_BINPATH == "foo" ]
    echo "It's foo"

will die with a strange error: “line 2: [: =: unary operator expected”. Problem is that when evaluating it will see if [ == “foo” ] and turns out [ is some reserved command or some such. The fix is to wrap $PHP_BINPATH in double quotes:

if [ "$PHP_BINPATH" == "foo" ]
    echo "It's foo"

Pass all the arguments!

When calling some other command from within the shell script, and all the arguments which are passed to the shell script need to be passed to that other command, use "$@“ for that:

$PHP_BINPATH usher.php "$@"

This will pass all the arguments to the PHP script which is called from within the shell script.

Happy hackin’!

Benchmarking pages behind a login with ab

published on November 09, 2011.

Tonight I decided to relax a bit and what better way of relaxing is there for a geek then to do some bash scripting?! So for fun and no profit I decided to try and benchmark pages with ab, Apache HTTP server benchmarking tool, which are behind a login. Turns out, it’s pretty easy after reading some man pages ;)

ab's help pages gives a few possible leads. We can POST data with the -p option, which would be great if we would like to benchmark the login process itself. But, we want to test the page after the login. So we’ll need the ab’s -C option, which allows for passing cookies in cookie-name=value pairs.

The login process itself is done with curl as it allows us to POST data to a server and store cookies received from the server in a cookie jar. curl writes the cookies in a Netscape cookie file format, whatever that is. Sample line is:

# Netscape HTTP Cookie File
# This file was generated by libcurl! Edit at your own risk.	FALSE	/	FALSE	0	PHPSESSID	[RANDOM_SESSION_ID]

From this output we’re interested in the [RANDOM_SESSION_ID] cookie value, as the cookie name is simply PHPSESSID and we can just hard-code it. To get the value, we use some obscure *nix magic: grep and cut. grep to grep the line with the PHPSESSID cookie and cut to cut out the 7th column from that line. Easy!

Now that we have the value of the cookie, we just pass it along with ab and done! We’re benchmarking pages behind a login.

The entire script is:



echo "Logging in..."

curl -c $COOKIE_JAR -d username=user -d password=h4x0r

echo "Getting the session id..."
PHPSESSID=$(cat $COOKIE_JAR | grep PHPSESSID | cut -f 7)

echo "The session id is:"
echo "=================="

ab -n 10 -c 10 -C PHPSESSID=$PHPSESSID

The script is also on Github here.

Tip: use ab’s -v option to test for HTTP codes and/or redirects to see if you are really on the page you want to be.

Happy hackin’!

Robert Basic

Robert Basic

Software developer making web applications better.

Let's work together!

I would like to help you make your web application better.

Robert Basic © 2008 — 2020
Get the feed