CURL: ebay_example.sh PDF Print E-mail
Wednesday, 23 September 2009 19:57
#!/bin/bash
#
# 1. Visit eBay's main page.
# 2. "Click" the sign-in link.
# 3. Sign in using a given user name and password.
# 4. Visit the "My eBay" page for that user.
# 5. Report success or failure.
#

# Some variables to make stuff simpler
# where is curl?
CURL="/usr/local/bin/curl"

# User-Agent (Firefox on MacOS X), built this way to break the lines
# without inserting a newline character
UA="Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US;"
UA="${UA} rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"

# Our cookie jar
JAR="cookies.txt"

# The eBay credentials we'll use
USER=my-eBay-User
PASS=my-eBay-Password

# if our cookie jar exists, kill it. That's part of test setup.
[ -f ${JAR} ] && rm -f "${JAR}"

# We'll use a variable called 'step' to keep track of our progress. It will
# allow us to determine where we fail, if we fail, and it will serve as a
# convenient way to name files.
typeset -i step
step=1

# First things first: Visit the main page, pick up a whole basket of cookies
echo -n "step [${step} "
${CURL} -s -L -A "${UA}" -c "${JAR}" -b "${JAR}" -e ";auto" \
-o "step-${step}.html" http://www.ebay.com/
if [ $? = 0 ]; then
step=$step+1
echo -n "OK] [${step} "
else
echo "FAIL]"
exit 1
fi

# Next, click the sign-in link to bring up the sign-in page.
# Observation tells us that this non-SSL link usually results in a 300-series
# redirection. We'll use -L to follow the redirection and fetch the other
# page, too.
${CURL} -s -L -A "${UA}" -c "${JAR}" -b "${JAR}" -e ";auto" \
-o "step-${step}.html" \
'http://signin.ebay.com/ws/eBayISAPI.dll?SignIn'
if [ $? = 0 ]; then
step=$step+1
echo -n "OK] [${step} "
else
echo "FAIL]"
exit 1
fi

# Now login. This is a post. Observation tells us that this probably
# results in a 200-series "OK" page, when successful. We should probably
# figure out what happens on failure and handle that case, huh?
${CURL} -s -L -A "${UA}" -c "${JAR}" -b "${JAR}" -e ";auto" \
-d MfcISAPICommand=SignInWelcome \
-d siteid=0 -d co_partnerId=2 -d UsingSSL=1 \
-d ru= -d pp= -d pa1= -d pa2= -d pa3= \
-d i1=-1 -d pageType=-1 -d rtmData= \
-d userid="${USER}" \
-d pass="${PASS}" \
-o "step-${step}.html" \
"https://signin.ebay.com/ws/eBayISAPI.dll?co_partnerid=2&siteid=0&UsingSSL=1"

if [ $? = 0 ]; then
step=$step+1
echo -n "OK] [${step} "
else
echo "FAIL]"
exit 1
fi

# Prove we're logged in by fetching the "My eBay" page
${CURL} -s -L -A "${UA}" -c "${JAR}" -b "${JAR}" -e ";auto" \
-o "step-${step}.html" \
'http://my.ebay.com/ws/eBayISAPI.dll?MyEbay'

if [ $? = 0 ]; then
echo "OK]"
else
echo "FAIL]"
exit 1
fi

# Check the output of the most recent step. Our userid will appear in
# the HTML if we are logged in. It will not if we aren't.
count=$(grep -c ${USER} step-${step}.html)
if [ $count -gt 0 ]
then
echo -n "PASS: ${USER} appears $count times in step-${step}.html"
else
echo "FAIL: ${USER} does not appear in step-${step}.html"
fi