Use curl to Monitor Your Vonage Phone Bill
If you're a Vonage 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:
- Determine the login URL (the page with the login form on it).
- Open the login page in your browser.
- Use the browser's "View Source" option to look at the HTML and locate the login form. Make note of the URL that the form posts to and the names of the fields that get posted. Generally there are only 2 fields: username and password (although they might have different names), but there may also be some hidden fields that you need to include (as well as understand what they are and what is an appropriate value to send).
- Retrieve the login page with curl. Note, that it may not actually be necessary to retrieve this page but if the page that the form posts to expects some cookies to be set, you may need to retrieve this first page so that you can create and save the cookies (which curl does for you).
- Invoke curl a second time and post the login data.
- Now invoke curl a third time passing the URL of the actual page that you want to retrieve.
- If the site has a logout link you can optionally also use curl a fourth time to retrieve the logout page to ensure that the session is closed.
The curl command below retrieves the Vonage login page:
curl --silent --cookie-jar $cookie_jar \ --output $web_page-1 \ http://www.vonage.com/?login
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 \ http://www.vonage.com/?login 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/.*>\$//' -e 's/<.*//')
Mitch Frazier is an Associate Editor for Linux Journal.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Control Your Linux Desktop with D-Bus
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Google's SwiftShader Released
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide