Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 11-18 of 18 total

Searching through your Twitter archive

This Ruby code downloads previous twitter entries as html files to a local file directory.
(1..5).each {|i| `wget --user=jrobertson --password=secret http://twitter.com/account/archive?page=#{i}`}

then using grep -i <keyword> * you can search for anything you've twittered in the past.

Note: Use Wget sparingly.

Convert from HTML to XHTML with HTML Tidy

This HTML Tidy example converts an html file into an xml file.

tidy -asxhtml -numeric < index.html > index.xml


example found from Tip: Convert from HTML to XML with HTML Tidy [ibm.com]

Wget's switch to preserve the filename

Download a file and preserve the filename. Without the switch -O the next time the file is downloaded the new filename would be projxmlbase.rb.1

wget -O projxmlbase.rb http://xml.mysqueak.info/p/projxmlbase.rb

Create a crontab entry using XML

This code creates a formatted crontab entry using the files crontab.xml, and crontab_txt.xsl. It is intended that all cron jobs could be stored in the crontab.xml file and then passed to the crontab.

file: crontab_txt.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

  <xsl:template match="crontab_entry">

  <xsl:apply-templates select="minutes"/>
  <xsl:apply-templates select="hours"/>
  <xsl:apply-templates select="day_of_month"/>
  <xsl:apply-templates select="months"/>
  <xsl:apply-templates select="day_of_week"/>

  <xsl:value-of select="command" />
  </xsl:template>
  
  <xsl:template match="minutes">
    <xsl:apply-templates select="minute"/>
    <xsl:if test="not(minute) or minute=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="hours">
    <xsl:apply-templates select="hour"/>
    <xsl:if test="not(hour) or hour=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_month">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_week">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="months">
    <xsl:apply-templates select="month"/>
    <xsl:if test="not(month) or month=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
    
  <xsl:template match="month">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="day">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="hour">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="minute">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template name="format_value">
    <xsl:value-of select="."/>
    <xsl:if test="not(position() =last())"><xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="format_unit">
    <xsl:call-template name="recurring" />
    <xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template name="recurring">
    <xsl:if test="@every">/<xsl:value-of select="@every"/>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>

file: crontab.xml
<crontab_entry>
  <minutes/>
  <hours every="3">
  <day_of_month>
    <day>3</day>
    <day>10</day>
    <day>15</day>
  </day_of_month>
  <months/>
  <day_of_week/>
  <command>ssh james@192.168.1.220 runjob.sh</command>
</crontab_entry>

output: * */3 3,10,15 * * ssh james@192.168.1.220 runjob.sh

A helpful crontab.xml template
<cron>
  <minutes>
    <!-- minute (0 - 59) -->
    <minute></minute>
  </minutes
  <hours>
    <!-- hour (0 -23) -->
    <hour></hour>
  </hours>
  <day_of_month>
    <!-- day of month (1 - 31) -->
    <day></day>
  </day_of_month>
  <months>
    <!-- month (1 - 12) -->
    <month></month>
  </months>
  <day_of_week>
   <!-- day of week (0 - 6) (Sunday=0 or 7) -->
   <day></day>
  </day_of_week>
  <command></command>
</cron>

Using Ruby to delete a file directory

In this example the file we want to delete is a non-empty directory called sample.
  require 'fileutils' 

  FileUtils.rm_r 'sample'

copy files using rsync and ssh

source: http://www.mikerubel.org/computers/rsync_snapshots/#Abstract ; switches: -a = archive mode -e specifies the remote shell to use

rsync -a -e ssh source/ username@remotemachine.com:/path/to/destination/

Rip an Icecast stream

Rip an Icecast stream (for a 30 min duration), and direct the streamripper text including errors to /dev/null. Note: Directing the output to null is necessary when running this within a cron job.

streamripper http://192.168.1.7:8000/radio_x.ogg.m3u -dq  ~james/Streamripper_rips/hay -a %d -l 1800 >/dev/null 2>&1

Launch Windows XP with Qemu

This code worked under Ubuntu Server 7.4, using Qemu and a Windows XP image file.

# Starts up QEmu with various options

# Variables
set -o errexit
SOUND="-soundhw es1370"
MEMORY="-m 330"
TIME="-localtime"
IMGPATH="/mnt/data/virtual/xp"
IMG=$1
USERID=`whoami`

# need to make sure we have read/write access to the  device at /dev/net/tun
echo "Using sudo too to setup /dev/net/tun..."
sudo chown root:$USERID /dev/net/tun
sudo chmod g+rw /dev/net/tun
# now be sure that IP forwarding and masquerading are good to go
echo "Need root password to setup IP forwarding..."
su -c "echo '1' > /proc/sys/net/ipv4/ip_forward"
echo "Done being root..."
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# Create tap interface so that the script /etc/qemu-ifup can bridge it
# before qemu starts
echo "Using sudo to setup the tap interface..."
IFACE=`sudo tunctl -b -u $USERID`

NET="-net nic,vlan=0 -net tap,vlan=0,ifname=$IFACE,script=/etc/qemu-ifup"

# QEmu start
qemu ${SOUND} ${TIME} ${MEMORY} ${IMG} ${NET} -usb


# QEmu has stopped - no longer using tap interface
sudo tunctl -d $IFACE &> /dev/null

# user no longer needs read/write on /dev/net/tun
sudo chown root:root /dev/net/tun



after script execution I run this 'sudo ifconfig tap0 192.168.1.6'
when the script is finished I run this 'sudo tunctl -d tap0'
« Newer Snippets
Older Snippets »
Showing 11-18 of 18 total