Thursday, June 05, 2014

Lifesaver Snippets

Universal JDBC CLI Client

I work on the Unix/Linux boxes running Java application servers regularly. One of the problems I always face is that access to the server is restricted, yet occasionally I have the need to access the most protected resources of all - the database. Without direct access to the database server, all my GUI database client tools are useless. What is available to me is only the JVM in an SSH session.

I find that, while I can't install any database tools on the servers without going through a lot of bureaucratic red tapes, I can usually make use of what is already available on the server to do my work. With only h2, the JDBC driver and some shell script, I am able to connect to the database and get the data I want. Here's how.

First, put all the jar files into the same folder. You should at least have h2 and the JDBC driver in the folder. Create a script h2shell:

#!/bin/bash

CP=.
for j in *.jar; do
  CP=$CP:$j
done

java -Xmx512m -classpath $CP org.h2.tools.Shell "$@"

Make the script executable:

chmod +x h2shell

Next, create a helper script with the database connection properties. Let's call the script mydb:

#!/bin/bash

./h2shell -url url -user username -password password

Make the script executable too:

chmod +x mydb

You can now connect to the DB by running mydb:

./mydb

telnet substitute

Due to the security policy of one of my clients, they remove the telnet program from all of their servers. telnet is one of common tools to test network connectivity. I found a crude alternative by writing some Python scripts.

import telnetlib

t = telnetlib.Telnet('some.host', port)
print t.read_eager()
t.write(message)
print t.read_all()
t.close()

Besides telnetlib, Python has a lot of other wonderful modules such as urllib, socket.

Saturday, June 08, 2013

How to calculate MD5 as zero-padded hex string in Groovy?

import java.security.MessageDigest
 
def digest = MessageDigest.getInstance("MD5")
 
def bytes = data.getBytes()
def md5 = digest.digest(bytes)
 
// Convert to hex, left-padded with 0 to 32 chars
def hex = new BigInteger(1, md5).toString(16).padLeft(32, "0")

Gist: https://gist.github.com/sjtai/5733901

Tuesday, January 22, 2013

How to invoke JavaScript in JMeter?

A colleague complained that there is little documentation about how to invoke JavaScript in JMeter. Specifically, his problem was about how to use the session ID to compute a value to be passed to the server. After playing with JMeter for an hour, we managed to figure it out. This is how it works:

Since the session ID (JSESSIONID) is set as a cookie, we only need to figure how to retrieve the cookie in JMeter and pass it to the JavaScript. The first step is to enable cookies in JMeter so that they are accessible by using variables. In jmeter.properties, set CookieManager.save.cookie=true.

Next, make sure you add a HTTP Cookie Manager to your Thread Group. Add a BSF PostProcessor and write your JavaScript in the text area provided. You can also externalize your script to a file and specify the file name in the text field above the text area. You can access the JSESSIONID cookie this way:
var sid = "${COOKIE_JSESSIONID}";

To invoke a JavaScript function and set the return value as a variable:
var val = compute(sid);
vars.put("someKey", val);

You can access the value later as ${someKey}.

Tuesday, January 15, 2013

How to extract a range of lines from a large file?

It is not advisable to use vi to view a large file as on most system vi will create a swap file of similar size. It is better to extract the lines of interest to another file. Say you want to extract lines 20001 to 30000 from a log file. There are many ways to accomplish that and I am going to show you 2 of them here.

sed (my preferred way)
sed -n -e '20001,30000p' log.txt > /tmp/log-small.txt
-n means don't print anything unless the p command is used. -e means execute the following expression (those inside '....'). 20001,30000p means for lines 20001 to 30000 inclusive, execute the command p, which prints them to the console.

the head & tail way
head -30000 log.txt | tail -10000 > /tmp/log-small.txt

The head command will output lines 1 to 30000 and pipe to tail which will print the last 10000 lines, i.e. lines 20001 to 30000.
As you can see, the head & tail way requires some mental arithmetics and so it is not my preferred way.

Wednesday, January 09, 2013

How to find out Solaris version number?

Depending on what information you want, the following commands will provide different information about a Solaris server:
  • cat /etc/release
  • showrev
  • uname -a

Saturday, January 05, 2013

How to Get IP Address from MAC

One way to get the IP address of a remote host in the same subnet given a MAC address is by using the "arp -an" command. However, this will only work if the we have previously connected to the remote host.

With the help of nmap, we can find out the IP address. For example, the command below will scan the 10.1.1.0/24 subnet with nmap. After the scan we search through the output of arp. If the remote host that we are searching for is up, its MAC address will be displayed together with its IP address.

$ nmap -sP 192.168.0.0/24 > /dev/null
$ arp -an | grep -i "12:34:56:78:90:ab"
? (192.168.0.105) at 12:34:56:78:90:ab [ether] on wlan0

Saturday, October 27, 2012

The amazing date command

I use the Linux date command regularly. Normally I use it to format current date, e.g.
date +"%Y-%m-%d"
Yesterday, while writing a backup script, I found out that the command also understands simple English. E.g.
date +"%Y-%m-%d %H:%M:%S" --date "yesterday"
date +"%Y-%m-%d %H:%M:%S" --date "-7days"
date +"%Y-%m-%d %H:%M:%S" --date "-7days 00:00:00"
date +"%Y-%m-%d %H:%M:%S" --date "2 weeks ago"
This helps when I want a date that represents, say, 3pm yesterday.