Executing Programs in Java

The program arguments will be executed through this ‘shell’. You can make a loop that will make the following snippet a real shell.

import java.io.*;

public class SimpleShell {
	public static void main(String[] argv) {
	String line;
	StringBuilder output = new StringBuilder();

	try {
		Process p = Runtime.getRuntime().exec(argv);
		BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
		while ((line = input.readLine()) != null) {
			output.append(line).append("\n");
		}

		System.out.println(output);
	} catch (Exception e) {
		e.printStackTrace();
	}

	}
}

SVN: “Attempted to lock an already locked dir”

If you work with Eclipse and SVN, you’ll probably come across an error similar to: Attempted to lock an already locked dir

You can sometimes resolve this by cleaning up the workplace directly in eclipse by right clicking on your project -> team -> cleanup.

However, if that fails, you will need to delete all the lock files in your workplace.

If you’re under linux, run find ./ -name lock -exec rm {} \; in the workplace directory. If you’re on windows, I’m guessing you can do a search for all lock files and deleting it from the search results. Once that’s done, try to commit again, and hopefully it will work.

If eclipse still refuses to stop spewing error messages after all that, it’s best you start over your workplace. That is, back up your workplace, delete it, re-checkout the code, copy the backed up code into the workplace again, refresh the workplace in eclipse, and attempt to recommit again.

If even that fails, then it’s time to gg.

Installing and Creating a new SVN Repository

Note: This is the second time I’m writing this entry since RZ somehow managed to lose my first post. So these steps are from memory and might not be 100% correct.

If you want to install subversion, you will need to have apache. Easiest way to install this is via yum (or whatever package manager you need to use).
yum install subversion mod_dav_svn httpd

Once complete, you should have a directory in /etc/httpd/conf.d/subversion.conf.

We will create the repos in /home/subversion. I’ll also make a user ‘subversion’ who can manage these files without root. As root, run the following commands:

  1. useradd subversion
  2. cd /home/subversion

To create a new repo:

  1. Create the directory for the new repo. eg: 'repo'
  2. run 'svnadmin create repo'

To create the initial import of the trunk:

  1. mkdir /tmp/trunk
  2. svn import /tmp/trunk file:///home/subversion/repo/trunk -m "Initial Import"

You may want to add users to access this repo. Suppose you want to add user:

  1. cd /home/subversion/repo/conf
  2. htpasswd -cm svn-auth first_user
  3. htpasswd -m svn-auth second_user


Note: do not use the -c argument after adding the first user, otherwise it will overwrite the existing password file.

You will then need to add an entry to /etc/httpd/conf.d/subversion.conf with the following:


        DAV svn
        SVNPath /home/subversion/repo

        AuthType Basic
        AuthName "Subversion Repo for MyRepoName"
        AuthUserFile /home/subversion/repo/conf/svn-auth
        Require valid-user

Ensure the files in /home/subversion/ are owned by apache, but still is group owned by subversion.

You should now be able to access your new repo by going to /svn/repo after using the username/password combination given to htpasswd above.

Generating Self-Signed SSL Certs

If you want to have SSL enabled on your webserver, you may want to generate self signed certificates.

  1. cd ~
  2. mkdir ssl
  3. openssl genrsa -des3 -out server.key 4096
  4. openssl req -new -key server.key -out server.csr
  5. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  6. openssl rsa -in server.key -out server.key.insecure
  7. mv server.key server.key.secure
  8. mv server.key.insecure server.key

Step 3: Generating the key, use any pass phrase.
Step 4: Creating the certificate signing request
Step 5: Signing the certificate signing request
Step 6: Making a key that has no pass phrase

Note: Ensure that all keys are not readable to others. chmod 700 the ssl directory and chmod 600 all the keys.


To install the key on a litespeed web server

  1. create a new listener, call it for example, ‘secure’
  2. go to the ssl tab and specify the following values:
    1. Private Key File: $SERVER_ROOT/ssl/server.key
    2. Certificate File: $SERVER_ROOT/ssl/server.crt
    3. Chained Certificate: no
    4. CA Certificate Path: $SERVER_ROOT/ssl
    5. CA Certificate File: Not Set
  3. Copy the entire ssl directory generated above to the litespeed server root
  4. Chmod the entire ssl directory to 700, and chmod the keys to 600
  5. Chown/Chgroup the entire ssl directory to the lsadm user

Java Quick RegEx Matching

Suppose you want to match something in Java such as the average time from a ping output:

Pinging steamr.com [64.22.123.253] with 32 bytes of data:
Reply from 64.22.123.253: bytes=32 time=100ms TTL=53
Reply from 64.22.123.253: bytes=32 time=100ms TTL=53
Reply from 64.22.123.253: bytes=32 time=101ms TTL=53
Reply from 64.22.123.253: bytes=32 time=100ms TTL=53

Ping statistics for 64.22.123.253:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 100ms, Maximum = 101ms, Average = 100ms

You would want to do something like:

Pattern pingPattern = Pattern.compile(".*Minimum = [0-9]+ms, Maximum = [0-9]+ms, Average = ([0-9]+)ms.*", Pattern.DOTALL);
Matcher pingMatch   = pingPattern.matcher(pingOutput);

if (pingMatch.matches()) {
	averagePing = Integer.parseInt(pingMatch.group(1));
}

Note that the ‘.’ in the pattern does not by default match new lines or return carriages. The Pattern.DOTALL is needed so that ‘.’ accepts everything. RTFM for more info.

Logging in via SSH keys

Let user@ServerA be the host that wants to connect to user@ServerB.

Run the following commands as user@ServerA

ssh-keygen -t rsa          # use empty pass phrase, save the keys to ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub
chmod 700 ~/.ssh           # set the permissions of the .ssh directory to 700.
chmod 600 ~/.ssh/*         # set the permissions of the keys so no one else can read them.

It’s important to set the permissions! Otherwise SSH will not use the keys without a warning or message. I’ve wasted enough time to learn this the hard way.

Copy the ~/.ssh/id_rsa.pub from user@ServerA to user@ServerB:~/.ssh/authorized_keys. Again, set the permissions.

chmod 700 ~/.ssh         # set the permissions of the .ssh directory to 700.
chmod 600 ~/.ssh/*       # set the permissions of the keys so no one else can read them.

Compiling & Installing vnstat-1.10

Run the following commands to install vnstat. This will also configure vnstat to listen on eth0 and start automatically on boot.

This was done in CentOS5.4, but it should also work for any *NIX based machines.

Note: You may want to check on the vnstat website for the latest build. At the time of this writing, the latest version is 1.10.
Note2: The following example will configure vnstat on eth0; You may want to change this if you are on a OpenVZ VPS to venet0.

wget http://humdi.net/vnstat/vnstat-1.10.tar.gz
tar -xzf vnstat-1.10.tar.gz
cd vnstat-1.10
make
make install
vnstat --testkernel
/bin/cp examples/init.d/centos/vnstat /etc/init.d/vnstat
ln -s /etc/init.d/vnstat /etc/rc3.d/S40vnstat
vnstat -u -i eth0
/etc/init.d/vnstat start