全部博文(106)
分类: Web开发
2014-10-04 06:43:33
http://www.linuxjournal.com/content/use-curl-monitor-your-vonage-phone-bill
If you're a user and you'd like to keep tabs on your
bill as the month progresses, the script described here can help.
The script uses curl to login to your Vonage account and
download the web page with your current balance.
The balance is then extracted using grep and sed.
Downloading web pages with curl is fairly easy, it gets a bit tricky though when you need to login to a web site before you can get to the web page you want to download. The basic sequence of steps to get to a page behind a login page using curl is:
The curl command below retrieves the Vonage login page:
curl --silent --cookie-jar $cookie_jar \ --output $web_page-1 \
Note the --cookie-jar option, this stores any cookies required by the website in the specified file. The file to store the retrieved page is specified by the --output option.
The curl command below now posts the login data required by the login form:
curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --location \ --data "username=$username&password=$password" \ --output $web_page-2 \ https://secure.vonage.com/vonage-web/public/login.htm
Notice here that in addition to --cookie-jar option we also specify the --cookie option. This tells curl to use the cookie jar that we created in the first invocation as input for this invocation. We also, specify the --location option so that any redirects sent by the page are followed. The actual data to post is specified with the --data option. The values before the equals signs in the data are the fields names from the login form, the values after are the appropriate field values to post.
The following two curl commands now retrieve the billing page and logout from Vonage:
curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --output $web_page-3 \ https://secure.vonage.com/webaccount/billing/index.htm
curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --location \ --output $web_page-4 \ https://secure.vonage.com/webaccount/public/logoff.htm
All that's left now is to extract the account balance from the billing page (the third page that we retrieved). After looking at the returned HTML, I was able to see where the data I wanted was located and determine a way to filter out all the extraneous information using grep and sed:
echo Phone bill: $(grep 'td_value_total_amount' $web_page-3 | sed -e 's/.*>\$//' -e 's/<.*//')
The following shows a sample run of the script:
$ sh check-vonage.sh
Phone bill: 12.50
The entire script follows:
#!/bin/bash cookie_jar=cookies.tmp web_page=vonage.tmp username=USERNAME password=PASSWORD trap "rm -f $cookie_jar $web_page-*" EXIT curl --silent --cookie-jar $cookie_jar \ --output $web_page-1 \ curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --location \ --data "username=$username&password=$password" \ --output $web_page-2 \ https://secure.vonage.com/vonage-web/public/login.htm curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --output $web_page-3 \ https://secure.vonage.com/webaccount/billing/index.htm curl --silent --cookie $cookie_jar --cookie-jar $cookie_jar \ --location \ --output $web_page-4 \ https://secure.vonage.com/webaccount/public/logoff.htm echo echo echo Phone bill: $(grep 'td_value_total_amount' $web_page-3 | sed -e 's/.*