MySQL Show or Desc(ribe) All Tables at Once

I was recently getting up to speed on a few new applications and needed to understand the database schema. I found myself repeatedly using the MySQL show tables and desc tables command line queries. After doing this a few times I got a little frustrated so I wrote this handy little shell script that will generate HTML output of an entire database schema to db_schema.html on your desktop.

Example Output - db_schema.html

This script uses the mysql command line “SHOW TABLES” and “DESC <TABLE>” commands.

For those not familiar with the “SHOW tables” command here’s what the output looks like-

mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |

For those not familiar with the “DESC Table” command here’s what the output looks like.


mysql> desc user;
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |

This script assumes that:

  • the mysql username is root
  • there is no password
  • the db name is dbname


if [ -f ~/Desktop/db_schema.html ]; then
rm ~/Desktop/db_schema.html
fi

for i in `mysql -B -N -uroot dbname -e ’show tables;’`; do
echo "<a href=’#$i’>$i</a><br>" >> /tmp/$$.nav.html
echo "<a name=’$i’></a>" >> /tmp/$$.tables.html
echo "<h2>$i</h2>" >> /tmp/$$.tables.html
mysql –html –column-names -uroot dbname -e "desc $i;" >> /tmp/$$.tables.html
echo -n >> /tmp/$$.tables.html
done;

cat /tmp/$$.nav.html /tmp/$$.tables.html > ~/Desktop/db_schema.html

Posted in Development, Linux Admin | Tagged , , , , , , | Comments closed

BeautifulSoup Demo - screen scraping quickly with Python

From the BeautifulSoup website:

Beautiful Soup is a Python HTML/XML parser designed for quick turnaround projects like screen-scraping. Three features make it powerful:

1. Beautiful Soup won’t choke if you give it bad markup. It yields a parse tree that makes approximately as much sense as your original document. This is usually good enough to collect the data you need and run away.
2. Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. You don’t have to create a custom parser for each application.
3. Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don’t have to think about encodings, unless the document doesn’t specify an encoding and Beautiful Soup can’t autodetect one. Then you just have to specify the original encoding.

Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. You can tell it “Find all the links”, or “Find all the links of class externalLink”, or “Find all the links whose urls match “foo.com”, or “Find the table heading that’s got bold text, then give me that text.”

Valuable data that was once locked up in poorly-designed websites is now within your reach. Projects that would have taken hours take only minutes with Beautiful Soup.

Getting Started

1. Install BeautifulSoup

2. Create a script that uses BeautifulSoup

from BeautifulSoup import BeautifulSoup
import re
import urllib2

# download the page
response = urllib2.urlopen("http://www.boattrader.com/search-results/Type-any/Make-mako/Length-17,25/Zip-02445/Radius-100/Sort-Length:DESC/ ")
html = response.read()

# create a beautiful soup object
soup = BeautifulSoup(html)

# all links to detailed boat information have class lfloat
links = soup.findAll("a", { "class" : "lfloat" })
for link in links:
print link['href']
print link.string

# all prices are spans and have the class rfloat
prices = soup.findAll("span", { "class" : "rfloat" })
for price in prices:
print price
print price.string

# all boat images have attribute height=105
images = soup.findAll("img",height="105")
for image in images:
print image			# print the whole image tag
print image['src']	# print the url of the image only

Helpful resources:

Posted in Development | Tagged , , , , | Comments closed

SSH Tunnels Are Like a Free Cheap VPN

If you’re not already familiar with the concept I bet you’d be very interested to learn that you can simulate VPN access to a network as long as you have access to a linux host on that network.

These instructions work on OS X and Linux. Putty also has tunneling capabilities, but those are beyond the scope of this post.

There are many uses for the technology.

SSH Tunneling Applications

  1. Get online at a client site - If you’re visiting a client site and don’t have proxy access you can use a SSH SOCKS proxy to bypass the LAN proxy and get on the web
  2. Encrypt traffic - to keep LAN admins from seeing what you’re looking at
  3. Bypass corporate web filters - bypass corporate firewall to get access to Gmail, Facebook and other restricted resources.
  4. Defeat QOS limitations - Bypass rate limiting and other service caps imposed by QOS rules
  5. Access your database server - Access your database server even though remote access is disabled.

Access your database server

Port forward Postgresql


ssh -f root@server.yourdomain.com -L 5432:localhost:5432 -N

Access the web using a Socks Proxy

$ssh -D 9999 username@ip-address-of-ssh-server

Then go to Firefox->Preferences->Advanced->Network->Settings to configure the socksproxy. The address is localhost.

Further Reading on SSH Tunneling

Posted in Linux Admin, Networking, Postgresql | Tagged | Leave a comment

Google Docs Turns a Blind Eye to Security

Google doesn’t use SSL for the Google Calendar or for Google Docs. When information is sent to and from Google the information is sent in clear text and is visible at all intermediate network routers.

I know that Google Docs is very convenient, but make sure not to store sensitive data on the Google Docs system.

You can read up on Google’s security at the link below.  I feel their document is a little misleading as it doesn’t fully disclose the face that information is not encrypted while in transit.

http://www.google.com/apps/intl/en/business/infrastructure_security.html

Posted in Security | Tagged , | Leave a comment

Use nslookup to check DNS propagation and verify MX record configuration

NSLookup is a tool that can be immensely helpful when troubleshooting network connectivity issues. Here are some example uses:

1.) You want to see if the DNS change you requested in live on the primary nameserver

hopper-macbook:~ andrewhopper$ nslookup
> server dns1.noris.net
Default server: dns1.noris.net
Address: 213.95.0.65#53
> set type=a
> puma.com
Server: dns1.noris.net
Address: 213.95.0.65#53

Name: puma.com
Address: 128.167.119.25

2.) You just configured Google apps for your domain and want to verify MX records have been properly configured.

hopper-macbook:~ andrewhopper$ nslookup
> set q=mx
> hopper.biz

Server: 208.59.247.45
Address: 208.59.247.45#53

Non-authoritative answer:
hopper.biz mail exchanger = 20 alt1.aspmx.l.google.com.
hopper.biz mail exchanger = 20 alt2.aspmx.l.google.com.
hopper.biz mail exchanger = 30 aspmx2.googlemail.com.
hopper.biz mail exchanger = 30 aspmx4.googlemail.com.
hopper.biz mail exchanger = 30 aspmx3.googlemail.com.
hopper.biz mail exchanger = 30 aspmx5.googlemail.com.
hopper.biz mail exchanger = 10 aspmx.l.google.com.

Authoritative answers can be found from:
hopper.biz nameserver = NS2.SLICEHOST.NET.
hopper.biz nameserver = NS3.SLICEHOST.NET.
hopper.biz nameserver = NS1.SLICEHOST.NET.
aspmx.l.google.com internet address = 209.85.221.60
alt1.aspmx.l.google.com internet address = 72.14.247.27
alt2.aspmx.l.google.com internet address = 209.85.222.78
aspmx2.googlemail.com internet address = 209.85.135.27
NS1.SLICEHOST.NET internet address = 67.23.4.57

Posted in Linux Admin, Networking | Tagged , , | Leave a comment

Macbook Pro Hard Drive Replacement

My beloved Macbook keeps running out of space so I keep putting bigger hard drives in it.  The latest is a fabulously large 500GB Hitachi drive. The drive runs quiet and cool. Make sure to buy your new harddrive from Other World Computing. They have a great selection, great shipping policies, and good prices.

The process will void your warranty but is pretty straight forward thanks to the phenomenal instructions here-iFixIt Macbook Pro 15 Hard Drive Replacement Guide

Posted in Hardware | Tagged | Leave a comment

Postgres Update from function

I always forgot the syntax of the Postgresql update from functionality
UPDATE employees SET sales_count = sales_count + 1 FROM accounts
  WHERE accounts.name = 'Acme Corporation'
  AND employees.id = accounts.sales_person;
Posted in Postgresql | 6 Comments

Screen keyboard shortcuts

Screen is an amazing little utility that allows you to have multiple sessions going within one terminal session.

Here’s a link to the man page - http://man.cx/screen

Here’s a quick cheatsheet borrowed from http://www.pixelbeat.org/lkdb/screen.html

Key Action Notes
Ctrl+a c new window
Ctrl+a n next window I bind F12 to this
Ctrl+a p previous window I bind F11 to this
Ctrl+a “ select window from list I have window list in the status line
Ctrl+a Ctrl+a previous window viewed
Ctrl+a S split terminal horizontally into regions Ctrl+a c to create new window there
Ctrl+a :resize resize region
Ctrl+a :fit fit screen size to new terminal size Ctrl+a F is the same. Do after resizing xterm
Ctrl+a :remove remove region Ctrl+a X is the same
Ctrl+a tab Move to next region
Ctrl+a d detach screen from terminal Start screen with -r option to reattach
Ctrl+a A set window title
Ctrl+a x lock session Enter user password to unlock
Ctrl+a [ enter scrollback/copy mode Enter to start and end copy region. Ctrl+a ] to leave this mode
Ctrl+a ] paste buffer Supports pasting between windows
Ctrl+a > write paste buffer to file useful for copying between screens
Ctrl+a < read paste buffer from file useful for pasting between screens
Ctrl+a ? show key bindings/command names Note unbound commands only in man page
Ctrl+a : goto screen command prompt up shows last command entered
Posted in Linux Admin | Tagged | 4 Comments

Learn VIM in 5 Minutes

I found a great Stack Overflow question on learning VIM here @
http://stackoverflow.com/questions/2573/vim-tutorials

VIM 5 Min Tutorial

"Here is your 5 minute tutorial. The easiest way to learn vi is to know what the letters stand for:"
y(ank) - copy
d(elete) - delete
c(hange) - change
p(aste) - put from buffer after cursor
o(pen) - start a new line
i(nsert) - insert before current character
a(fter) - insert after current character
w(ord) - moves to beginning of next word
b(ack) - moves to beginning of current word or prior word
e(end) - moves to end of current word or next word
f(ind) - moves to a character on the current line
movement keys you just need to learn: h,j,k,l

^ - beginning of text on a line
$ - end of text on a line
0 - first position on line

most commands can be prefaced with numeric modifiers.
2w - means move 2 words
5h - means move 5 charcters to the left
3k - means move 3 lines up
3fs - means move to the 3rd letter s folling the cursor

modification commands (d,c,y) need to know how much to work on.
dd - delete a line into memory
yy - yank a line into memory
cc - change the whole line
c$ - change from current position to the end
c2w - change the text spanning the next 2 words
3dd - delete 3 lines
d2f. - delete to the second period.

. - means redo the last modification command.
/ - searches for text, and then n(ext) will go the next found occurance. N will go prior.
? - searches backwards through the document.

Further Reading about VIM

  1. http://rayninfo.co.uk/vimtips.html
  2. http://blog.interlinked.org/tutorials/vim_tutorial.html
  3. http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
Posted in Linux Admin | Tagged , | Leave a comment