ssh launcher

st3ve: Here is a handy little program that I wrote one afternoon, that you can use to launch ssh sessions quickly from the system tray. Replace the XFCE-specific `exo-open' command with the command for your desktop environment.

You must have the system tray extension installed.

 #!/usr/bin/env tclsh

 package require Tk
 package require Freedock



 #
 # presets
 #
 set hosts_file ~/.sshlauncher_hosts
 set opener "exo-open --launch TerminalEmulator"




 #
 # create a little tray icon
 #
 proc make_icon {} {

   dockwin .dock
   pack [button .dock.b -text ssh> -padx 0 -pady 0 -font "[font actual Arial] -size 8" -command show_menu]

   menu .menu -tearoff 0 -font "[font actual Arial] -size 8" -bd 1

 }



 #
 # show/hide the menu
 #
 proc show_menu {} {

   if { [winfo ismapped .menu] } {
     .menu unpost
   } else {
     .menu post [winfo rootx .dock] [expr {[winfo rooty .dock] - [winfo reqheight .menu]}]
   }

 }



 #
 # update menu
 #
 proc update_menu {} {
   global hosts opener

   #
   # delete all current entries, then update the menu
   #
   .menu delete 0 last

   .menu add command -label "Add a host" -command add_host
   .menu add separator
   foreach entry [lsort -dictionary -index 0 $hosts] {
     set host [lindex $entry 0]
     set user [lindex $entry 1]
     .menu add command -label $host -command "exec $opener \"ssh [expr {[string length $user] ? "-l $user" : ""}] $host\""
   }

 }



 #
 # show the add-host window
 #
 proc add_host {} {
   global hosts

   #
   # if the window is not already open, open it and lay out using grid
   #
   if { [lsearch [winfo children .] .add] == -1 } {

     toplevel .add -padx 3 -pady 3
     wm title .add "Add a new host"

     label .add.hl -text Host: -font "[font actual Arial] -size 8"
     entry .add.he -bd 1

     label .add.ul -text User: -font "[font actual Arial] -size 8"
     entry .add.ue -bd 1 -width 10

     grid .add.hl .add.he
     grid .add.ul .add.ue
     grid configure .add.hl .add.ul -sticky e
     grid configure .add.he .add.ue -sticky w
     grid columnconfigure . {0 1} -uniform allTheSame


     #
     # create a little save command
     #
     set save_cmd {
       if { [string length [.add.he get]] } {
         lappend hosts [list [.add.he get] [.add.ue get]]
       }
       save_hosts
       destroy .add
     }

     bind .add.he <Key-Escape> { destroy .add }
     bind .add.he <Key-Return> $save_cmd

     bind .add.ue <Key-Escape> { destroy .add }
     bind .add.ue <Key-Return> $save_cmd

     focus .add.he

   }

 }




 #
 # load hosts
 #
 proc load_hosts {} {
   global hosts_file hosts

   if { [file exists $hosts_file] } {
     set hosts_fh [open $hosts_file r]
     set hosts [gets $hosts_fh]
     close $hosts_fh
   } else {
     set hosts {}
   }

   update_menu

 }



 #
 # and save hosts
 #
 proc save_hosts {} {
   global hosts_file hosts

   set hosts_fh [open $hosts_file w]
   puts $hosts_fh $hosts
   close $hosts_fh

   update_menu

 }




 #
 # initialize the program
 #
 make_icon
 load_hosts
 wm withdraw .

RLH This is a neat little program. Just saying...