VNC Address Book Python

I work as an IT system administrator and use VNC daily to connect to internal machines. I find VNC to be the easiest way to manage all of the internal machines without having to physically sit in front of each machine when it needs to be serviced.

I run Ubuntu as my desktop at work and home, and I have yet to find a VNC management system that I like. Terminal Server Client works just find, but their "Quick Connect" feature just doesn't cut it. Remote Desktop Viewer works fine with .vnc files, but the interface is awful. TightVnc is by far my favorite program, but there is no "address book" type feature, plus it has to be run from the command line. So to solve my problems I decided that I needed to write a GUI interface that would act like an address book to run my VNC connections through TightVnc.

At the beginning of this project I knew nothing about Python, but from what I read I knew it would be powerful enough to handle what I needed it to do. Here is a screen shot to show you what the final product looks like (yes, all names have been changed to protect the innocent):



Let's take a look at the code.

Here's the beginning of the code where we declare arrays and variables:

#!/usr/bin/env python

import pygtk # this is pythons GUI interface
pygtk.require('2.0')
import gtk # to use gtk functions
import fnmatch # needed for string functions
import os # needed to sort through os files
rootfolder=os.getenv("HOME") # find home directory
homefolder=rootfolder + "/VNC/" # where VNC files live
dirList=os.listdir(homefolder) # find all files in VNC folder
dirList.sort() # sort the files alphabetically
cntr=[0,1,2,3]
storeList=["10_*","20_*","30_*","40_*"]
storeName=["North","South","East","West",]

This code was written 100% for me and my purposes. I'm sure it is portable, but understand that it was written to make my IT system administration life easier. I have a directory under my home folder called "VNC" - this folder contains standard .VNC files that I created for everyone in the company. In this example there are four different groupings (North South East West) that correspond to numbers (10, 20, 30, 40). So in my VNC folder you would see files called:
10_Doe_John.vnc, 20_Cartman_Eric.vnc and so forth.

These standard VNC files have one key element that we need (the connection IP address):

host=192.168.2.10

In the next section of the code we get into the meat of the program:

class AddressBook:
     def close(self, widget, event, data=None):
          gtk.main_quit()
     
     def okay_click(self, widget, data=None):
          data = open(data).readlines() # open the file
          sstring1="host=192.168" # basic host string
          for line in data:
               if sstring1 in line:
                    host = line[5:] # discard first host= characters
                    host = host.replace ( '\r\n', '' ) # remove line endings
          os.system("xvncviewer " + host) #run xvncviewer with new string
               
     def __init__(self):
          self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
          self.window.set_title("VNC Connections")
          self.hbox = gtk.HBox(False, 0)
          self.window.connect("delete_event", self.close)
          self.window.add(self.hbox)

          for cnt in cntr:
               self.vbox = gtk.VBox(False, 0)
               self.hbox.add(self.vbox)
               self.label = gtk.Label(storeName[cnt])
               self.vbox.pack_start(self.label, False, False, 2)
               self.label.show()
               for fname in dirList:
                    if fnmatch.fnmatch(fname, storeList[cnt]):
                         fstring=fname.replace ( '0_', '0-' )
                         fstring=fstring.replace ( '_', ', ' )
                         fstring=fstring.replace ( '.vnc', '' )
                         self.button = gtk.Button(label=fstring)
                         self.button.connect("clicked", self.okay_click, homefolder + fname)
                         self.vbox.pack_start(self.button, False, False, 2)
                         self.button.show()
               self.vbox.show()
          self.hbox.show()
          self.window.show()

Here's a basic overview of what the sections do:
The "def close" section is what the program does when you click on the close button.
The "def okay_click" section is what happen when you click on someone's name to connect to their computer. The program goes into the VNC file and extracts the host= portion of the file and formats a xvncviewer session to open a connection to that computer.
The "def __init__(self):" is the visual aspect of the program. Basically the program iterates through the /VNC/ folder and formats buttons for each person's name.

The last bit of code completes the program;

if __name__ == "__main__":
     AddressBook()
     gtk.main()

I've found that the easiest way to get started with programs like this would be to look at examples of working programs and try to understand what each different section does. I still have a very basic understanding of Python programming, but this exercise helped me understand how powerful the program really is. In the future I can see many more applications where Python will come in handy.


JLandis (Thursday 4th of March 2010 10:04:33 AM)

comments (0) | permalink


©2010 blog.jlandis.org