Showing posts with label vncserver. Show all posts
Showing posts with label vncserver. Show all posts

Tuesday, November 11, 2008

How to check if VNCClient is connecting to VNCServer

I was having the problem of the vncserver having too many 'alive' connections because vncclients were being killed abruptly and there is no build in methodology for vncserver that kills the vncserver if nobody is connecting to it. I was toying around with TCPKeepAlive and stuffs, but that didn't really work out fine.

One of the ways to do so if you have access to both the VNCServer and VNCClient codes is to use the ping-pong approach. In the sense that the VNCClient will constantly contact the VNCServer to tell him that it exist. If it does not for after a predefined time limit, you can take for granted that there is either a network problem of VNCClient is not listening to the server anymore. Building it is really quite simple:

1.) create a program call "vncinform" on the remote server. Basically it receives a request from a unique identifier (i.e. an ip address). and update the timestamp of a file.

2.) modify the vncviwer to call "vncinform" remotely using ssh. For example SSH vncinform 192.168.1.102. This basically tells the host that the host is active by updating the timestamp.

3.) on the end of the vncserver, create a cron job to kill the vncserver session if the timestamp for a particular host is more than a predefined amount of time.

Done!

Thursday, November 6, 2008

Establish SSH Tunnel and run VNCViewer using Perl

Using perl, I was trying to run SSH Tunnel first and then run VNCViewer through this tunnel. Of course it was not so simple as to just doing the following in perl:

#!/usr/bin/perl
system("ssh -l -L 10000:localhost:5901 ");
system("vncviewer localhost:10000");

If you try to do the above, it won't work! This is because the process gets stuck on the first line while in SSH. Basically if the tunnel is active, it can't possibly run the next command which is to launch vncviewer. But if the tunnel is terminated, so that we can run vncviewer, vncviewer won't work because there is no more binding on port 10000.

We need to use perls' build in fork function. Similar to running programs using the trailing & command in C. My implementation (thanks to estabroo from LQ) is as follows:

$pid = fork();
if ($pid == 0) {
system("ssh -l -L 10000:localhost:5901 ");
exit(0);
} elsif ($pid > 0) {
sleep(5);
system("vncviewer localhost:10000");
} else {
print "fork failed\n";
}

Basically what this does is, fork the process, execute SSH, wait for login and exit, sleep for 5 seconds, run the child process vncviewer. Done :)