Westward Ho                 
automatic recent ssh hosts quicklist for ubuntu 11.04
2012-01-03 07:51:59
back to category
  [P][L][E][C][T]
 
Hello Peeps,

i wrote this little ditty to give me automatic lists of hosts i have recently ssh'ed to as a quicklist that lives behind my gnome-terminal icon in the ubuntu dash/unity thing.

#!/usr/bin/perl # a script that reads your bash_history for ssh sessions and adds them to a # quicklist for the terminal icon in your ubuntu 11.04 unity desktop # Amias Channer 03/01/2012 # this script might rape your dog or steal your tofu , read and understand it first and don't blame me , patches also welcomed at me@amias.net # location of template, unity bar launchers and ssh host list $tertempl="/usr/share/applications/gnome-terminal.desktop"; $target="/home/amias/.local/share/applications/gnome-terminal.desktop"; $get_ssh_host_list=`cat ~/.bash_history | egrep -E '^ssh (*.)' | uniq -u`; # backup if file allready exists if (-e "$target" ) { print "Creating backup of: $target \n"; system ("mv -n $target $target.bak"); } else { print "WARNING: $target doesn't exist , yet"; } # copy template if it exists if (-e "$tertempl") { my $copy_command = "cp $tertempl $target"; print "Running copy: $copy_command \n"; system("cp $tertempl $target"); } else { die ("$tertempl doesn't exist"); } # open target and make sure we are at the end of the file open (TARGET,">>$target") or die ("cannot open $target"); seek(TARGET,0,SEEK_END); # get ready to loop through ssh hosts my $count=0; # count the amount of hosts we find my $buffer; my @rows = split(/\n/,$get_ssh_host_list); # loop through ssh hosts foreach my $line (@rows) { $count++; $buffer .= " [SSHA$count Shortcut Group] Name=Recent: $line Exec=gnome-terminal -x $line TargetEnvironment=Unity "; push (@shortcutlist,"SSHA$count"); }; # output results to gnome-terminal launcher as a quicklist print TARGET "\n"; print TARGET $buffer; close TARGET; # remove old Ayatana shortcuts line and replace with our updated list , this is done outside perl with perl , yes i know. my $flat_list_of_shortcuts = join(';',@shortcutlist); my $inplace_replace="perl -pi -e 's/X-Ayatana-Desktop-Shortcuts=New/X-Ayatana-Desktop-Shortcuts=New;$flat_list_of_shortcuts/' $target"; system("$inplace_replace");

ideally you should run this at login , you will also need to restart unity to see the differences (for some reason) but this is ok because the history file is only updated when you logout anyway. if you can find a more rapid way to do this i'd love to know , hit the comments or email me.

Toodle-pip Amias
 
Add a comment

  
The following comments on this article where made by users of this site :
This site and contents are copyright Amias Channer , see also www.amias.net