Howto: Insert Bash Command Output Into MySQL

Posted 1388 days ago - Development, Free Stuff

A BlogStorm reader emailed me today,

Hello,

I am replying to your post on http://www.blogstorm.co.uk/how-to-scrape-pages-with-coldfusion/

Wanted to see your experience in page scraping, may need your help on a project. HOw much did you do beyond the above Code?

Thanks

That guest post was written over a year ago and I haven't written anything in Coldfusion for quite some time. After responding with this information, he replied with,

Hello Guy,

Thanks for the info. We don't need something fast and efficient, since Bash or perl is faster probably. Just something I can maintain. Do you have a script that can extract the WHois info?

Thanks

So I put together a little sh/bash script that will read a list of domain names from a text file, whois each domain, and insert the results into a MySQL table. Basically a MySQL insert from bash. Here's the finished (tested/working) script:

#!/bin/sh
WHOISME="/tmp/list-of-domains-to-whois.tmp"
# mysql username
SQLUSER="root"
# mysql password
SQLPASS="h0h0h0"
# mysql server hostname/IP
SQLHOST="mysql-server.localdomain"
# prints date in YYYY-MM-DD format
SQLDATE=$(date +%F)
# database to use (created with: mysql> create database db_whois;)
SQLDB="db_whois"
# table inside the database to use
# created with: mysql> CREATE TABLE tbl_scraped(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(id),domain VARCHAR(50),whois_data TEXT,date DATE);
SQLTBL="tbl_scraped"
# mysql> describe tbl_scraped;
# +------------+-------------+------+-----+---------+----------------+
# | Field      | Type        | Null | Key | Default | Extra          |
# +------------+-------------+------+-----+---------+----------------+
# | id         | int(11)     | NO   | PRI | NULL    | auto_increment |
# | domain     | varchar(50) | YES  |     | NULL    |                |
# | whois_data | text        | YES  |     | NULL    |                |
# | date       | date        | YES  |     | NULL    |                |
# +------------+-------------+------+-----+---------+----------------+
# 4 rows in set (0.00 sec)

# the magic
cat $WHOISME |while true
do read LINE || break
WHOISD=$(whois $LINE |sed "s/'/\\\'/")
echo "INSERT INTO $SQLTBL VALUES (NULL,'$LINE','${WHOISD}','${SQLDATE}');"\
|mysql -u$SQLUSER -p$SQLPASS -h$SQLHOST $SQLDB
done; echo "Job Done - Exiting..."
exit 0

Enjoy...

Word Count: 350

Tags: , , , , , ,

Click Here to Submit a Comment

Permalink / Last Modified:

Support Nullamatix.com:

See Also:

  • 02/11/2008 -- Howto: Setup cron Jobs to Restart Lighttpd & MySQL
    Excerpt: "Over the past couple weeks MySQL crashed when spiked with large amounts of traffic. To remedy this, a cron job has been implemented to simply restart mysql and lighttpd every other day. Here's how it's done. First, if you haven't already, define a cron job ..."
  • 10/23/2009 -- DIY: Home Surveillance System with VLC
    Excerpt: "The current state of the global economy has shot the U.S. unemployment rate up - waay up. As a result, more people are willing to commit crimes in order to provide for themselves or their family. Just this year, two houses that share the street I live on were ..."
  • 04/11/2010 -- Howto: XCache in a Lighttpd Chroot on Debian
    Excerpt: "Whether you're pressed for resources on a virtual/dedicated server, or simply looking for ways to improve web application performance, XCache is guaranteed to produce the desired result. Within minutes of installing XCache: page load times were cut in half, ..."
  • 07/31/2009 -- Howto: Lock Your Screen in dwm with WindowsKey+L
    Excerpt: "A couple months ago I gave up all those fancy buttons, panels, widgets, icons, and other miscellaneous crap that make using a computer annoying, for a more simplistic approach. The dynamic window manager from suckless provides anything the casual web surfer ..."

Leave a Reply