Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, May 14, 2011

Emacs Batch Edit Example

Say the following function is in your .emacs file and lets you indent your C source code to your liking:



But you don't want to manually open each file into an Emacs buffer to execute this script. Emacs batch editing to the rescue:



The parameters load up my .emacs file as normal and runs the given eval. Apply a little command line magic and you should be able to exploit Emacs from the command line in a very efficient way.

Monday, January 17, 2011

Monitor Comcast Usage Data

The good folks at Comcast have decided to put a 250GB cap on monthly usage as a direct assault against my beloved Roku.



I decided to put a script together to notify me in case I start approaching the monthly limit. My first thought was that this was a perfect task for mechanize, a Ruby library for interacting with web sites. I put that aside, however, to make an attempt to script the scraping with curl.

Those folks at Comcast are whack. When logging in to the home page, what is sent back is a redirect - no not an HTTP redirect. You are sent back a page that has a form in it with a "cima.ticket" that submits itself in the body onload event. Here is the somewhat functional script to pull the your Comcast bandwidth usage:



I say somewhat functional because it doesn't work every time. It will fail to pull a result every now and then. There is a lot of wonkiness in the way the Comcast login works - and it appears this results in some kind of timing issue. Hopefully I have a chance to investigate a solution for this in the future.

Oh - back to mechanize. I googled for another solution to this problem and found this. It shows off the elegance and ease of use of mechanize, however, it seems to fail intermittently just like my script.

Sunday, February 01, 2009

Backup Blogger Blog with cURL Script

I read Dave Winer's Where's Your Data? post today which was in response to a blog post by Craig Burton title The State of Blogging Sucks. These articles refer to the problems that may arise when someone else is hosting your data. The problem could be a massive data loss (see Ma.gnolia), a company going out of business (apparently a Userland product?), or just data lock in.

With that in mind I thought about my blog here. This is primarily a dumping ground for small but handy techniques I learn so that if I figure out a way to do something - I can quickly google for it and use it again later. At this point there are enough helpful hints in here that I would hate to lose it. Blogger provides a way to export your blog as XML but it involves logging in and clicking a button - the last thing a programmer wants to have to remember to do on a recurring basis.

So...I cooked up this bash cURL script to backup a Blogger blog. This script demonstrates the basics of using cURL. The first curl command fetches the login page and pulls out a security token using grep and sed. The second curl command logs in, providing the security token, login, and password among other arguments. The next curl request requests the home page so that your blogID can be determined, which will be used in the final request. The final curl request actually requests the backup dumping the result to standard out - you can redirect this to the file of your choice or modify the script to also accept an output file. Here is the script:



In other news the Cardinals almost pulled it off today. This was the first time in a long time where I really wanted one team to win over the other in the Super Bowl. Alas it wasn't meant to be. I've been waiting for the Chiefs to make it for almost 25 years and it doesn't look I'll be rooting for them in the Super Bowl anytime soon.

Saturday, May 10, 2008

Tunnel Behind a Firewall to Connect with rdesktop (or mstsc)

Ok, ok, so we really have a couple of Windows computer at work that are being used to serve up Rails applications. They are firewalled so they don't accept rdesktop connections over their public IP. We do, however, have a trusty Linux box running an ssh server that has access to these Windows boxes via our internal network. This makes it possible to work from home, restarting mongrels and such, by tunneling through the Linux server. First you need to set up the tunnel. This is done as follows:



That is, create a tunnel through linux.box.name.com that connects to the internal network computer 10.0.6.13 on port 3389. Here, I have also set up the local port to be 3389 because it is the default rdesktop port and thus I have one less thing to specify to the rdesktop command. After this tunnel is set up you can connect simply:



To do this from a Windows machine you will need to install cygwin or putty to set up the tunnel and of course the command to connect is then mstsc.

Saturday, April 19, 2008

ImageMagick Bulk Image convert Fun

ImageMagick can do an amazing number of command line manipulation functions. ImageMagick's convert command, for example, can convert an image from one format to another and do a bunch of transformations along the way. Recently I needed to convert a whole bunch of SVG files deeply nested in a directory structure into transparent PNGs. convert, combined with a little bash magick, makes this a quick and painless task:



Let's break that down.


  1. Recursively find every file ending in .svg and pass each one separately to exec

  2. For each exec invoke a shell that executes a command from the given string with the filename getting passed as ${1}

  3. In the command call convert, changing white in the SVG to transparent in the PNG where the first argument is the SVG to convert, and the second argument is the destination PNG filename



Job complete.

Friday, March 21, 2008

Truncate all Tables in a MySQL Database

UPDATE: I've written a new post with an updated script that encourages safer password handling. You should go there instead.

Here is a handy script to truncate all the tables in a MySQL database. I use this sometimes to wipe out an entire database before loading in some production data to replicate a customer problem:



This script isn't perfect -- it should really prompt the user for his password so that these passwords don't get into bash history logs. In a later post I'm going to talk about using this command in conjunction with some command line MySQL command to load a remote database locally for testing.