Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created October 19, 2010 14:49
Show Gist options
  • Save slinkp/634313 to your computer and use it in GitHub Desktop.
Save slinkp/634313 to your computer and use it in GitHub Desktop.
horrible ssh wrapper hack that changes background of currently running roxterm window based on hostname.
function sshwrap {
# A wrapper for ssh that sets roxterm's background image based on the
# hostname.
# We do most of this in bash because it handles wrapping interactive
# commands and TTYs well, which Python does not (at least not without
# jumping through major hoops a la Pexpect).
# The bits that I could not bear to do in bash are done in inline Python.
# Yes, this is hideous.
# Leaves behind large numbers of nearly-identical Roxterm profiles; since I never select
# profile by hand, I don't care.
# Does not work everywhere ssh does, notably `RSYNC_RSH=sshwrap` does not work.
DEFAULT_PROFILE=kermit # This needs to exist already
FONT=/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Bold.ttf
ssh-agent-add
function generate_image {
IMG_DIR=~/.hostbackgrounds
mkdir -p ${IMG_DIR}
IMG_PATH=${IMG_DIR}/${1}.png
export IMG_PATH
if [ -f "${IMG_PATH}" ]; then
# Already got an image for this hostname
return 0
fi
# Try Googling for a useful image. Python helps.
TEMP_IMAGE=`python - "${IMG_PATH}" "${1}" <<EOF
import sys
import urllib2, urllib
import simplejson
import tempfile
imgpath, hostname = sys.argv[1:]
hostname = hostname.replace('.', ' ')
url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&'
url += urllib.urlencode({'q': hostname})
request = urllib2.Request(url, None, {})
response = urllib2.urlopen(request)
#Process the JSON string. If we get an image, save it to a temp file
#and print the temp file name.
results = simplejson.load(response)
results = results['responseData']['results']
if results:
best = results[0]
imgurl = best['url']
data = urllib2.urlopen(urllib2.Request(imgurl, None, {}))
outfile = tempfile.NamedTemporaryFile(delete=False)
outfile.write(data.read())
print outfile.name
EOF`
if [[ -z "$TEMP_IMAGE" ]]; then
# Didn't download anything, so just put the hostname in a png.
convert -background black -fill white -font "$FONT" -size 400x200 \
-rotate 20 "label:${TARGET_HOST}" "${IMG_PATH}"
else
# resize it, add a text label.
convert ${TEMP_IMAGE} -resize 500x500 \
-fill white -stroke black -strokewidth 0.2 -font "$FONT" \
-draw "scale 8,8 gravity center rotate 18 text 0,0 ${TARGET_HOST}" "${IMG_PATH}"
#convert ${TEMP_IMAGE} -size 500x500 -sigmoidal-contrast 10x50 -modulate 50 -fill white -font "$FONT" -draw 'rotate -20 text 100,100 "${TARGET_HOST}"' "${IMG_PATH}"
rm -f ${TEMP_IMAGE}
fi
}
if [ "$ROXTERM_ID" != "" ]; then
# Parse the hostname out of ssh args.
# This would be horrible to do in bash. So, we go python for this bit.
TARGET_HOST=`python - $@ <<EOF
import optparse
import re
opts, args = optparse.OptionParser().parse_args()
if args:
hostname = args[0]
if hostname.count('@'):
username, hostname = hostname.split('@', 1)
if hostname.count('.'):
if re.match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', hostname):
# Looks like an IP address.
pass
else:
parts = hostname.split('.')
if parts[0] == 'www':
parts = parts[1:]
if len(parts[-1]) < 4 and len(parts) >= 2:
# Drop trailing TLD.
parts = parts[:-1]
if parts:
# Assume the first remaining part is the part we care about.
# For example, with 'atat.openplans.org', we probably just want 'atat'.
hostname = parts[0]
print hostname
EOF`
echo Target is "$TARGET_HOST"
if [[ -n "${TARGET_HOST}" ]]; then
echo -n Setting up roxterm for ${TARGET_HOST}...
generate_image \"${TARGET_HOST}\"
ROXDIR=$HOME/.config/roxterm.sourceforge.net/Profiles
# Create a profile for this hostname.
rm -f $ROXDIR/${TARGET_HOST}
cp $ROXDIR/$DEFAULT_PROFILE $ROXDIR/${TARGET_HOST}
# Use the new profile, and tell it to use our image.
ROXPATH=/net/sf/roxterm/Options
ROXOPTS=net.sf.roxterm.Options
dbus-send --session $ROXPATH $ROXOPTS.SetProfile string:${ROXTERM_ID} string:${TARGET_HOST}
dbus-send --session $ROXPATH $ROXOPTS.StringOption string:Profiles/${TARGET_HOST} string:background_type 'string:1'
dbus-send --session $ROXPATH $ROXOPTS.StringOption string:Profiles/${TARGET_HOST} string:background_img "string:${IMG_PATH}"
# Adjust saturation to make it low-brightness.
dbus-send --session $ROXPATH $ROXOPTS.StringOption "string:Profiles/${TARGET_HOST}" "string:saturation" "string:0.19"
echo Done.
fi
fi
ssh $@
#Cleanup.
if [ "${TARGET_HOST}" != "" ]; then
dbus-send --session $ROXPATH $ROXOPTS.SetProfile "string:$ROXTERM_ID" string:${DEFAULT_PROFILE}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment