Posts Tagged ‘source code’

SSH with Java

July 8th, 2009

Recently I needed to do some server manipulation over the SSH-2 protocol from a Java client program. There are quite a few choices of SSH libraries for Java out there. Usually I prefer BSD license whenever possible, so I thought I’ll give Ganymed SSH-2 for Java a try. It turned out to be pretty simple to use. Here’s a short example of how to connect to the server using the private key and execute some command.

iBatis and Stored Oracle procedures/functions

September 1st, 2008

This one took me a while to get it right the first time. I won’t go into details of configuring iBatis datasources and such, and will go straight to putting some queries in the sqlMap file. Just let me note that I’m using iBatis 2.3 for these examples. I’ll start off with a procedure call.

<procedure id="getUserRoles" parameterMap="myParamMap">
    { call SCHEMA.GET_USERS_ROLES(?, ?) }
</procedure>

This one is pretty self-explanatory, just defining a procedure to be called. Notice the questionmarks in the SQL, don’t put the usual #variable# style annotation here. Also instead of parameterClass I use parameterMap here, which means I’ll have to define a parameter map for this query or it won’t work.

tail -f for windows

May 7th, 2008

Here’s a simple implementation of “tail -f” UNIX command equivalent in python. I used it a lot on windows servers to monitor log files. The good thing is that it works over SMB (windows shares).

#!/usr/bin/env python
import sys, os, time

def main(argv):
	if len(argv)<2:
		print "Usage: tail filename.log"
		exit(1)
	try:
		fp = open(argv[1], "r")
		st_results = os.stat(argv[1])
		st_size = st_results[6]
		fp.seek(st_size)

		while 1:
			where = fp.tell()
			line = fp.readline()
			if not line:
				time.sleep(1)
				fp.seek(where)
			else:
				print line,
		fp.close()
	except:
		fp.close()

if __name__=="__main__":
	main(sys.argv)

This is useful when starting/stopping remote services with Windows Service Controller:

Javascript introspection

April 18th, 2008

Very basic, but sometimes useful javascript object introspection

<img id="test">
<script type="text/javascript" language="JavaScript">
    el = document.getElementById('test');
    str = '';
    for (i in el) str += i+'; ';
    alert(str);
</script>

Strings in JAVA

April 17th, 2008

A few days ago I needed to extract all strings from .java files and also thought that it would be a good idea to keep count how many times a string is used. So I came up with this simple python script. It’s kind of a quick and dirty solution, but it met my needs for the particular task.