Skip to content

Instantly share code, notes, and snippets.

@lpar
lpar / pwgen.rb
Created June 17, 2011 18:10
Simple generation of readable random passwords using Ruby
#!/usr/bin/env ruby
# encoding: UTF-8
# Simple password generation in Ruby.
#
# Generate reasonably secure random passwords of any chosen length,
# designed to be somewhat easy for humans to read and remember.
# Each password has a capitalized letter and a digit.
#
# Example:
@lpar
lpar / utf8truncate.java
Created June 17, 2011 18:21
Truncate a Java string so that its UTF-8 representation will not exceed the specified number of bytes
/**
* Truncate a Java string so that its UTF-8 representation will not
* exceed the specified number of bytes.
*
* For discussion of why you might want to do this, see
* http://lpar.ath0.com/2011/06/07/unicode-alchemy-with-db2/
*/
public static String utf8truncate(String input, int length) {
StringBuffer result = new StringBuffer(length);
int resultlen = 0;
@lpar
lpar / weeknum.ls
Created June 17, 2011 18:28
ISO 8601 week number calculation in LotusScript
Function WeekNumber(t As NotesDateTime) As String
' Wrapper to work out week numbers of NotesDateTime objects in the
' current time zone
Dim lst As Variant
lst = t.LSLocalTime
WeekNumber = LSWeekNumber(lst)
End Function
Function LSWeekNumber(t As Variant) As String
' Returns the week number of a NotesDateTime object
@lpar
lpar / jdbcexample.java
Created June 17, 2011 18:38
An example of performing a JDBC query from Java and handling exceptions correctly
/**
* A correct example of performing a SQL query using JDBC.
*
* See http://lpar.ath0.com/2008/09/05/java-jdbc-and-memory-leaks/ for
* discussion; correct examples of Java JDBC code are surprisingly rare.
*/
public static void doSomething() {
try {
Connection connection = DriverManager.getConnection(JDBC_URL);
try {
@lpar
lpar / md5daemon
Created June 17, 2011 20:23
Automatic MD5 checksum generating daemon for use with vsftpd
#!/usr/bin/env ruby
# encoding: UTF-8
'di '
'ig00 '
# VSFTPD auto-MD5 program
#
# Run man -l <this file> for documentation
require 'rb-inotify'
@lpar
lpar / timeout.rb
Created June 17, 2011 20:41
Run a shell command in a separate thread, terminate it after a time limit, return its output
# Runs a specified shell command in a separate thread.
# If it exceeds the given timeout in seconds, kills it.
# Returns any output produced by the command (stdout or stderr) as a String.
# Uses Kernel.select to wait up to the tick length (in seconds) between
# checks on the command's status
#
# If you've got a cleaner way of doing this, I'd be interested to see it.
# If you think you can do it with Ruby's Timeout module, think again.
def run_with_timeout(command, timeout, tick)
output = ''
@lpar
lpar / jay
Created June 20, 2011 17:13
jay - a utility for removing excess kernel files from Ubuntu systems
#!/usr/bin/env ruby
# encoding: UTF-8
'di '
'ig00 '
# This is both a Ruby script and a man page; you can symlink it into your
# man directory as /usr/local/man/man8/jay.1 or run man -l on this file.
# The package which contains the kernel image itself
KERNEL_PACKAGE = 'linux-image'
@lpar
lpar / domino2syslog.rb
Last active August 29, 2016 16:14
Ruby code to pump IBM Lotus Domino server logs to regular Unix syslog
#!/usr/bin/env ruby
# encoding: UTF-8
# Script to take IBM Lotus Domino console input on stdin, and send it to
# syslog.
#
# Allows you to do all your logging via syslog, rather than having to
# keep weeks of data in log.nsf.
#
# In rsyslog, filter like this:
@lpar
lpar / Stopwatch.java
Created July 26, 2011 19:55
Stopwatch: Record real time taken for an operation, across multiple runs
import java.text.DecimalFormat;
/**
* The Stopwatch class provides a simple way to record the real-world time taken by an operation,
* averaged over multiple runs, and report the result easily in logs.
*
* An example use case would be timing JDBC calls to a relational database.
*
* This code does not measure CPU time or pay any attention to whether a thread or process is
* active or not. For that, you need to use a profiler.
@lpar
lpar / gist:1320801
Created October 27, 2011 20:42
Generic skeleton template for a command line utility written in Ruby
#!/usr/bin/env ruby
# encoding: UTF-8
'di '
'ig00 '
# This is both a Ruby script and a man page; you can symlink it into your
# man directory as commandname.1 or run man -l on this file.
# This is a generic skeleton for a Ruby command-line utility, showing how to
# make the same file work as both a Ruby script and a man page, for ease of
# distribution. This cool hack brought to you by mathew <meta@pobox.com>.