Skip to content

Instantly share code, notes, and snippets.

@RWJMurphy
RWJMurphy / gist:7225971
Created October 30, 2013 01:45
bash scripting helpers -- command line argument parsing, and some logging functions
function usage() {
cat <<EOD
$0 [-hnv]
-h, --help Print this text and exit
-n, --dry-run Dryrun; do not make any changes
-v, --verbose Enable verbose output; repeat for more verbosity
EOD
}
function debug() {
function retry() {
# retry [--limit,-l] [--delay,-d] [--] command args..
[ $# -lt 1 ] && echo "return [-ld] [--] commands [args...]" >&2 && return
LIMIT=5
DELAY=5
while [[ $# > 0 ]]; do
case $1 in
--limit|-l)
shift
@RWJMurphy
RWJMurphy / filerotator.py
Last active February 1, 2023 04:02
Python class and utility for file rotation. Configurable daily, weekly and monthly retention; can use hard links to save space; supports `argparse` config files.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ft=python ts=4 sw=4 expandtab
#
# Copyright (c) 2013 Reed Kraft-Murphy <reed@reedmurphy.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@RWJMurphy
RWJMurphy / __init__.py
Last active June 3, 2017 13:16
Fabric fabfile for turning a directory of scripts into remote-running tasks. Like magic. Just drop `__init__.py` into a folder containing your executable scripts, and `import script_dir` from your existing `fabfile.py`. Each script file will then be available as a task named `script_dir.script_name`
import os
import os.path
import re
import fabric.api
import fabric.tasks
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
SUDO_PREFIX = re.compile(r'/(root|sudo)_?', re.I)
@RWJMurphy
RWJMurphy / proc_exe_check.sh
Last active December 16, 2015 06:38
Checks for running processes whose executable is no longer on disk. Could indicate services that have been updated and need restarting, malicious processes, etc.
#!/bin/bash
function check_proc() {
pid=$1
link=$(readlink /proc/$pid/exe)
if [[ "$link" =~ \ \(deleted\)$ ]]; then
proc_launched=$(stat -c '%y' /proc/$pid)
binary=${link% (deleted)}
binary_stat=$(stat $binary | sed 's/^/\t\t/')
cmdline=$(cat /proc/$pid/cmdline | tr '\000' ' ')
@RWJMurphy
RWJMurphy / trac_attachment_clean.sh
Created April 5, 2013 05:57
Purges attachments from a trac instance, after a ticket has been closed for more than a given number of days.
#!/bin/bash -e
days_before_purge=30
tracdb="/var/www/trac/db/trac.db"
attachment_dir="/var/www/trac/attachments/ticket"
sqlite3="/usr/bin/sqlite3"
sql_closed="select distinct id from ticket join attachment using (id) where status = \"closed\" and datetime(changetime/1000000, 'unixepoch') <= datetime('now', '-${days_before_purge} days');"
closed_tickets=$(echo ${sql_closed} | ${sqlite3} ${tracdb})
for id in $closed_tickets; do
@RWJMurphy
RWJMurphy / omfg_konami.js
Created March 3, 2013 22:34
The worst idea.
function wat(elem){
var color = $.Color({
hue: Math.random() * 360,
saturation: 1,
lightness: 0.5
});
$(elem).animate( { color: color }, 500, function(){wat(elem)});
}
if (window.addEventListener) {
@RWJMurphy
RWJMurphy / rootkit_check.sh
Last active December 14, 2015 00:48
A basic check for the `libkeyutils` sshd rootkit.
#!/bin/bash
me=$0
die() {
echo $*
usage
exit 1
}
usage() {
@RWJMurphy
RWJMurphy / v0.34.11.ini
Created February 10, 2013 09:09
Dwarf Therapist memory layout for DF 0.34.11 on OSX
[info]
checksum=0x8f421f9c
version_name=v0.34.11
complete=true
[addresses]
translation_vector=0x01636a88
language_vector=0x01636a70
creature_vector=0x015f56ec
dwarf_race_index=0x015dcf80
reedm@www:~/tmp $ ls -ld .
drwx------ 2 reedm reedm 4096 Feb 4 14:54 ./
reedm@www:~/tmp $ cat > foo
This is a file
reedm@www:~/tmp $ chmod -w foo
reedm@www:~/tmp $ mv foo bar
reedm@www:~/tmp $ ls -la
total 12
drwx------ 2 reedm reedm 4096 Feb 4 14:54 ./
drwx-----x 54 reedm reedm 4096 Feb 4 10:18 ../