Convert Text To HTML Char Codes

I got tired of hand-formatting or shell-scripting a text converter for this blog to post code properly so I wrote this py script to do it for me. It converts a text file containing possibly used HTML characters into HTML character codes.

#!/usr/bin/python
import sys
s = ""
l = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
while (1):
	c = sys.stdin.read(1)
	if (not c):
		break
	o = ord(c)
	if (o == 13):
		continue
	elif ((o < 127) and (c not in l)):
		s += ("&#" + str(o) + ";")
	else:
		s += (c)
print(s)

Convert Text To HTML Char Codes

Non-Exiting Screen Startup

I was trying to run screen in the background with a python script inside of it, however, when the python was exiting due to coding errors, the screen was quitting too. I could have turned on logging but I didn’t want to deal with screen log files all the time. I wanted the screen session to remain open, even if the python quit so I could reattach to it and see what was going on (like a normal command prompt). Here was the solution I came up with:

/usr/bin/screen -dm /bin/bash -c 'echo "your program here" ; /bin/bash'

Run this to reattach it later:

/usr/bin/screen -dr
Non-Exiting Screen Startup

JavaScript Popup For The CDoT Dashboard

Below is a simple piece of JavaScript which will open a new pop-up window pointing to the CDoT dashboard that contains various interesting stats related to our work. You are able to change the height and width of the window by modifying the appropriate options below. In addition, you are able to disable features like the toolbar, menubar and statusbar in order to give the window a fullscreen-kiosk-like look and feel.

<script type="text/javascript">
	window.open("http://scotland.proximity.on.ca/jbuck/status/", "awesome", "width=1700, height=1900, \
toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no");
</script>
JavaScript Popup For The CDoT Dashboard

Simple RSS Application For OS X

Here’s an application I created for OS X which grabs new stories/articles that are posted to slashdot and reddit front pages (you can’t enter any site yet but it works with RSS). The app is meant to be run off focus (behind other windows) and it will keep a list of stories read in order to tell which stories are new. As you click on the new stories you want to read, they will disappear and once the window is put in the background, it will open the chosen articles in the default browser and remove the new stories from the list.

I created this app as it acts more like an RSS-diff program and simply tracks and monitors changes in RSS stories. The busier the feed the better as this program will basically keep a log of stories until you check them out. Let me know if you find this program useful and/or have any suggestions on how to make it better!

Feed App Download [via mediafire]

Simple RSS Application For OS X

Smarter Fedora Build Process (Styrene)

This project will hopefully allow for a smarter build process to take place when building Fedora which includes choosing a specific order for the packages destined to be queued. It should allow one to intelligently determine which packages need to be built first (if it has 0 dependencies or all of its dependencies already built). The main loop basically just performs passes through the list of unbuilt packages and seeing which ones meet these requirements and queuing them.



[replaced by a new script: koji-follow.py]

Smarter Fedora Build Process (Styrene)

Automatically Move Files From One Dir To Another

Here is a lame script that you can run in the background to automatically pick up files in one user’s home directory and place them on your desktop. Just run this as root and it will check to see if the user is no longer logged in first and if so, it takes ownership of any file found and moves it to your desktop.

#!/bin/bash

if [ "$2" == "" ]
then
	echo "Usage: $0 <source username> <destination username>"
	exit 1
fi

while true
do
	testoutp=`who | grep -i "$1"`
	if [ "$testoutp" == "" ]
	then
		find /home/$1 -type f -exec chown "$2:$2" "{}" \;
		find /home/$1 -type f -exec mv "{}" "/home/$2/Desktop/tmp/" \;
	fi
	sleep 5
done

Automatically Move Files From One Dir To Another

A CDoT Python IRC Bot

Below is a basic IRC bot written in python which grabs the builder bot statuses from the arm.koji website and sends a message to a channel if the enable or ready statuses change to “no”. The lbot.py code is a shared python library/module and various python bot scripts can use this module to achieve different tasks for IRC messaging/action.

Edit: Last updated on June 30, 2011

lbot.py

import os
import re
import signal
import select
import socket
import sys
import time
import urllib


def formtime():
	currsecs = time.localtime()
	
	return time.strftime("%H:%M:%S", currsecs)

def fakehand(s, f):
	pass

def readurls(urlnlist=[], filename="lbot.txt"):
	waittime = (5 * 60)
	
	while (1):
		outpstri = ""
		
		for urlnitem in urlnlist:
			signal.signal(signal.SIGALRM, fakehand)
			signal.alarm(waittime / 2)
			
			try:
				uobjdata = urllib.urlopen(urlnitem)
				tempstri = uobjdata.read()
				print("fetched [%d] bytes from [%s]" % (len(tempstri), urlnitem))
			
			except:
				outpstri = ""
				break
			
			outpstri += tempstri
			signal.alarm(0)
		
		if (outpstri):
			fileobjc = open("/tmp/%s" % (filename), "w")
			fileobjc.write(outpstri)
			fileobjc.close()
		
		time.sleep(waittime)

sockline_s = {}

def sockline(sockpref, sockobjc):
	global sockline_s
	
	if (not sockpref in sockline_s.keys()):
		sockline_s[sockpref] = ""
	
	timeread = 0.25
	chekstri = sockline_s[sockpref].find("\n")
	
	while (chekstri == -1):
		(readlist, sendlist, errolist) = select.select([sockobjc], [], [], timeread)
		
		if (sockobjc in readlist):
			tempread = sockobjc.recv(1024)
			
			if (len(tempread) == 0):
				print("socket error...")
				sys.exit(0)
			
			sockline_s[sockpref] += tempread
			chekstri = sockline_s[sockpref].find("\n")
		
		else:
			break
	
	chekstri += 1
	reslstri = sockline_s[sockpref][:chekstri]
	sockline_s[sockpref] = sockline_s[sockpref][chekstri:]
	
	if ((len(reslstri) > 0) and (sockpref != "")):
		outpstri = ("%s %s [RECV] %s" % (formtime(), sockpref, reslstri.strip()))
		print(outpstri)
	
	return reslstri

sockobjc = None

def socksend(sockpref, sendstri):
	global sockobjc
	
	tempstri = sendstri.strip()
	
	if ((len(tempstri) > 0) and (sockpref != "")):
		outpstri = ("%s %s [SEND] %s" % (formtime(), sockpref, tempstri))
		print(outpstri)
	
	sockobjc.send(sendstri + "\r\n")

sendtime = 0
sendwait = 1
sockpref = "IRC"
sendlist = []

def waitsend(sendstri=""):
	global sockobjc
	
	global sendtime
	global sendwait
	global sockpref
	global sendlist
	
	if (sendstri != ""):
		sendlist.append(sendstri)
	
	if (len(sendlist) > 0):
		prestime = time.time()
		difftime = (prestime - sendtime)
		
		if (difftime >= sendwait):
			socksend(sockpref, sendlist[0])
			sendlist.pop(0)
			sendtime = prestime

nickname = ""
chanblst = []

def ircbinit(nickstri, chanlist):
	global sockobjc
	global sockpref
	
	global nickname
	global chanblst
	
	hostname = "irc.freenode.net"
	hostport = 6667
	
	idenname = "alex"
	fillname = "bob"
	realname = "charlie"
	
	nickname = nickstri
	chanblst = chanlist
	
	sockobjc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sockobjc.connect((hostname, hostport))
	
	socksend(sockpref, "NICK %s" % (nickname))
	socksend(sockpref, "USER %s %s %s :%s" % (idenname, hostname, fillname, realname))

jointime = 0
joinwait = (5 * 60)

def ircbmain(pastdata, presdata):
	global sockpref
	global sockobjc
	
	global nickname
	global chanblst
	
	global jointime
	global joinwait
	
	# check for a valid socket object first
	
	if (sockobjc == None):
		print("null error...")
		return -1
	
	# set some shared method vars
	
	prestime = time.time()
	
	# read a line from the socket object and exit if error
	
	readdata = sockline(sockpref, sockobjc)
	waitsend()
	
	if (type(readdata) == type(-1)):
		sockobjc.close()
		print("socket error...")
		return -2
	
	readdata = readdata.strip()
	
	# if server ping the send pong back
	
	chekstri = readdata.find("PING :")
	
	if (chekstri == 0):
		chekstri = readdata.find(":")
		sendstri = ("PONG %s" % (readdata[chekstri:]))
		
		socksend(sockpref, sendstri)
		readdata = ""
	
	# delay send the join channel commands if connect or time
	
	chekstri = readdata.find("001 %s" % (nickname))
	difftime = (prestime - jointime)
	
	if ((chekstri != -1) or ((jointime != 0) and (difftime > joinwait))):
		for channame in chanblst:
			sendstri = ("JOIN %s" % (channame))
			waitsend(sendstri)
		
		jointime = prestime
		readdata = ""
	
	# check to see if we successfuly joined at least one channel
	
	regxobjc = re.match("^:?%s[^ ]* JOIN :.*" % (nickname), readdata)
	
	if (regxobjc):
		jointime = prestime
		readdata = ""
	
	# if we have not joined a channel yet then return
	
	if ((jointime == 0) or (not pastdata) or (not presdata)):
		return readdata
	
	# if list size was requested then send the length of the list
	
	regxobjc = re.match("^:?[^ ]* PRIVMSG ([^ ]*) :%s numb.*" % (nickname), readdata)
	
	if (regxobjc):
		sendstri = ("PRIVMSG %s :[#] %d" % (regxobjc.group(1), len(presdata)))
		socksend(sockpref, sendstri)
	
	# if a list tail was requested then send the specified number of items
	
	regxobjc = re.match("^:?[^ ]* PRIVMSG ([^ ]*) :%s tail(.*)" % (nickname), readdata)
	
	if (regxobjc):
		try:
			tailnumb = regxobjc.group(2).strip()
			tailnumb = (-1 * int(tailnumb))
		
		except:
			tailnumb = -1
		
		for presitem in presdata[tailnumb:]:
			sendstri = ("PRIVMSG %s :[*] %s" % (regxobjc.group(1), presitem))
			waitsend(sendstri)
	
	# print out any list diffs
	
	for presitem in presdata:
		if (presitem in pastdata):
			continue
		
		for channame in chanblst:
			sendstri = ("PRIVMSG %s :[+] %s" % (channame, presitem))
			waitsend(sendstri)
	
	return readdata

sbot.py

import os
import re
import sys
import time

import lbot

def fechfile(filename, urlnstri=""):
	resllist = []
	
	try:
		fileobjc = file("/tmp/%s" % (filename))
		readdata = fileobjc.read()
		fileobjc.close()
	
	except:
		return resllist
	
	readdata = readdata.replace("\t", "").replace("\r", "").replace("\n", "")
	readdata = readdata.replace("<tr", "\n<tr")
	readlist = readdata.split("\n")
	
	for readitem in readlist:
		regxobjc = re.match(".*<a href=\"hostinfo\?hostID=[0-9]*\">([^<]*cdot[^<]*)</a>.*<td>([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)</td>.*", readitem)
		
		if (not regxobjc):
			continue
		
		hostname = regxobjc.group(1)
		edthour = (int(regxobjc.group(5)) - 4)
		
		edthour = str(edthour)
		checkind = int(regxobjc.group(2) + regxobjc.group(3) + regxobjc.group(4) + regxobjc.group(5) + regxobjc.group(6))
		timesecs = (time.time() + (4 * 60 * 60) - (30 * 60))
		locltime = time.localtime(timesecs)
		presdate = int(time.strftime("%Y%m%d%H%M", locltime))
		
		if (checkind < presdate):
			hoststri = ("[%s] last check-in [%s-%s-%s %s:%s:%s]" % (hostname, regxobjc.group(2), regxobjc.group(3), regxobjc.group(4), edthour, regxobjc.group(6), regxobjc.group(7)))
			resllist.append(hoststri)
	
	return resllist

def ircbmain():
	if (len(sys.argv) < 4):
		print("Usage: %s <nick> - <chan 0> ... <chan N>" % (sys.argv[0]))
		sys.exit(0)
	
	nickname = ""
	chanlist = []
	
	x = 1
	l = len(sys.argv)
	f = 0
	
	while (x < l):
		if (sys.argv[x] == "-"):
			f += 1
		
		elif (f == 0):
			nickname = sys.argv[x]
		
		elif (f == 1):
			chanlist.append(sys.argv[x])
		
		x += 1
	
	p = os.fork()
	
	if (p == 0):
		templist = []
		
		for x in range(0, 3):
			templist.append("http://arm.koji.fedoraproject.org/koji/hosts?state=all&order=name&start=%d" % (x * 50))
		
		lbot.readurls(urlnlist=templist, filename=nickname)
		sys.exit(0)
	
	pastdata = []
	presdata = []
	
	lbot.ircbinit(nickname, chanlist)
	
	while (1):
		tempdata = fechfile(nickname)
		
		if (tempdata):
			presdata = tempdata
			
			if (not pastdata):
				pastdata = presdata
		
		ircdstri = lbot.ircbmain(pastdata, presdata)
		
		if (type(ircdstri) == type(-1)):
			break
		
		if (presdata):
			pastdata = presdata

ircbmain()

A CDoT Python IRC Bot

Sending An SSH Key To Multiple SSH Hosts

The script below will first create an SSH public/private key pair on the current, main host. It will then get your username and prompt for your password allowing the root user to add your account to the destination servers. Note: you must have a root login on each host. The ping statement is used to check to see if the host is up so we don’t waste time. This script will then wait on stdin (via cat with no arguments) and write the ssh key-file (via the piped data) to the temp directory. It will then add the username, set the password, create a ssh directory and move the transferred key file to it. At the end, it will then give ownership to the specified username.

#!/bin/bash

ssh-keygen

u=`whoami`
read -s -p "Password: " pass

for x in {1..7}-{1..5}
do
	ping -c 1 -W 1 $x > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		tempkeyf='cat > /tmp/authorized_keys'
		makeuser='useradd '$u
		makepass='printf "'$pass'" | passwd --stdin '$u
		makesshd='mkdir /home/'$u'/.ssh'
		copykeyf='mv /tmp/authorized_keys /home/'$u'/.ssh/'
		ownevery='chown -R '$u' /home/'$u'/.ssh'
		cat ~/.ssh/id_rsa.pub | ssh root@$x "$tempkeyf ; $makeuser ; $makepass ; $makesshd ; $copykeyf ; $ownevery"
	fi
done

Sending An SSH Key To Multiple SSH Hosts