[Home] [Blog] [Contact] - [Talks] [Bio] [Customers]
twitter linkedin youtube github rss

Patrick Debois

Sending Vnc Keystrokes Using Java

I have been experimenting with sending VNC keystrokes in different ways before . To finalize these experiments I decided to create a java library that is able to send keys towards a vnc server. I take advantage of existing logic of the vnc-tight project.

The project provides the source for a java based vncviewer, but the problem is that it uses an applet awt-based interface. I wanted it to be used from the commandline without the X requirement.

Presenting the new project jvncsender

The commandline version

usage: java -jar jvncsender.jar [-list] [-help] -host <hostname> -port <port> -text <text> [-password <password>] [-wait <seconds>]
 -help                  print this message
 -host <hostname>       hostname or ip-address to send it to
 -list                  list keymappings
 -password <password>   password to use
 -port <port>           port to connect to f.i. 5900
 -text <text>           text to send, (can be use multiple times)
 -wait <seconds>        seconds to wait in between sending different texts (default=1s)

text can also take special keys f.i. like "linux ks=ks.cfg<RETURN>"
use -list options to see all keymappings

or from within java:

try {
	VncSender vncSender=new VncSender(vncHost,vncPort,vncPassword); 
	vncSender.setVncWaitTime(timeInSec);	
	vncSender.sendText(vncText);						

} catch (Exception ex) {
	System.out.println(ex.toString());
}

I’ve been using it for automating kickstarts via VNC vmware.

F.i. my text string for kickstarting an ubuntu server without creating a custom cdrom would look like this:

vncText=new String[]{ "<ESC><ESC><RETURN>",
		"/install/vmlinuz noapic" +
		" hostname=pxeboot domain=jedi2.be netcfg/get_domain=jedi.be interface=eth0 netcfg/get_ipaddress=192.168.2.150 netcfg/get_netmask=255.255.255.0 netcfg/get_gateway=192.168.2.10 " ,
		" netcfg/get_nameservers=192.168.2.10 netcfg/disable_dhcp=true" ,
		" netcfg/choose_interface=eth0 netcfg/wireless_wep= preseed/url=http://192.168.2.30/<TILDE>patrick/preseed.cfg debian-installer=en_US auto locale=en_US kbd-chooser/method=us",
		" fb=false debconf/frontend=noninteractive", 
		" console-setup/ask_detect=false console-setup/modelcode=pc105 console-setup/layoutcode=us ",
		" initrd=/install/initrd.gz -- <RETURN>"
};

Note: this only works if your keyboard mapping is in us mode, as the keymappings are scancodes not actual ascii codes.

# to get all vnc code
$ svn co https://vnc-tight.svn.sourceforge.net/svnroot/vnc-tight vnc-tight
# to get all vnc java code
$ svn co https://vnc-tight.svn.sourceforge.net/svnroot/vnc-tight/trunk/java vnc-tight-java

I ended up using the jar file announced TightVNC: VNC Viewer with SSH Tunneling - April 2010

In order to get access to certain functions, I put my classes inside the same package. But because the jar file was signed, I needed to remove the signing:

$ wget http://www.tightvnc.com/download/tightvnc-1.5.1-jviewer-bin.zip
$ unzip tightvnc-1.5.1-jviewer-bin.zip
$ cd tightvnc-1.5.1-jviewer-bin
$ ls *.jar 
TightVncViewer.jar
$ mkdir newjar
$ cd newjar
$ jar -xvf ../TightVncViewer.jar
$ rm -rf META-INF
$ jar -cvf TighTVncViewer-unsigned.jar