Add known/configured SSH hosts to index

Sharing and discussing custom actions for LaunchBar

Add known/configured SSH hosts to index

Postby antifuchs » Mon Aug 03, 2009 1:47 pm

Hi there, I've just written a little script that lets me really quickly open an SSH connection to hosts that I have in my ~/.ssh/known_hosts or ~/.ssh/config files. This is very similar to the Quicksilver SSH plugin, and makes me a happy camper, so I thought I'd share it with the LaunchBar community. Note that this is a somewhat complex process, but we are power users after all. 8)

So, here's how it works: Terminal.app is associated with the ssh:// URL type; So, we make .webloc files for the hosts that SSH knows, and have LaunchBar index them.

First, make the script that will regularly create the webloc files for SSH hosts, name it "create-ssh-bookmarks.rb" put it in /usr/local/bin:

Code: Select all
#!/usr/bin/env ruby

## Customizable stuff:
$locations_dir = File.expand_path("~/Library") + "/SSH Locations"



require "fileutils"

$debug = ARGV[0] == '-d'

FileUtils.mkdir_p($locations_dir)
FileUtils.rm(Dir.glob($locations_dir + "/*"))

def make_webloc(hostname)
  loc_filename = $locations_dir + "/#{hostname} (ssh).webloc"
  begin
    File.open(loc_filename, 'w') do |file|
      file.write <<-XML
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
            <key>URL</key>
            <string>ssh://#{hostname}</string>
        </dict>
        </plist>
      XML
    end
  rescue Exception => e
    STDERR.write "Can't write webloc file #{loc_filename} for host #{hostname}: #{e}"
  end
end

# Known hosts:
['/etc/ssh/ssh_known_hosts', File.expand_path('~/.ssh/known_hosts')].each do |path|
  begin
    File.open(path).each do |line|
      hostname = line.split(',')[0]
      make_webloc(hostname) unless hostname.match(/ /)
    end
  rescue Errno::ENOENT => e
    puts "Can't open #{path}: #{e}" if $debug
  end
end

# SSH Config parsing:
['/etc/ssh/ssh_config', File.expand_path('~/.ssh/config')].each do |path|
  begin
    File.open(path).each do |line|
      if line.match /^\s*Host\s+(\S+)\s*$/
        host = $1
        make_webloc(host) unless host.match /\*/
      end
    end
  rescue Errno::ENOENT => e
  end
end


Then, in the terminal, type "crontab -e" and paste the following line:
Code: Select all
* * * * * /usr/local/bin/create-ssh-bookmarks.rb


This will update your SSH bookmarks every minute, so now open LaunchBar's index window: Index -> Show Index, hit the bottom-left + button, "Add Folder...", select ~/Library/SSH Locations, and you're done: Now you can activate LaunchBar, type parts of a hostname, and quickly open an SSH connection to that host.

Hope this helps, and massive props to obdev for making LaunchBar :)
antifuchs
 
Posts: 3
Joined: Mon Aug 03, 2009 12:24 pm

Re: Add known/configured SSH hosts to index

Postby antifuchs » Mon Aug 03, 2009 6:36 pm

One more thing: Instead of running this script from cron, you can skip the "crontab -e" step, and place a file named "net.boinkor.launchbar-ssh-indexer.plist" in ~/Library/LaunchAgents/, with these contents:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>net.boinkor.launchbar-ssh-indexer</string>
   <key>ProgramArguments</key>
   <array>
      <string>/usr/local/bin/create-ssh-bookmarks.rb</string>
   </array>
   <key>QueueDirectories</key>
   <array/>
   <key>RunAtLoad</key>
   <true/>
   <key>WatchPaths</key>
   <array>
      <string>/etc/ssh_known_hosts</string>
      <string>/etc/ssh_config</string>
      <string>/Users/asf/.ssh/config</string>
      <string>/Users/asf/.ssh/known_hosts</string>
   </array>
</dict>
</plist>


Note that you will have to change the locations so that they don't point to /Users/asf and to your home dir instead. This will pick up changes to your SSH configuration as soon as it's made: Add a host to your config, and you can immediately use it from LaunchBar. Thanks a lot to Pepi of maclemon.at for that suggestion.
antifuchs
 
Posts: 3
Joined: Mon Aug 03, 2009 12:24 pm

Re: Add known/configured SSH hosts to index

Postby Jay2k1 » Thu Nov 05, 2009 11:56 am

Thank you so much, that is so cool and useful.

I made my ssh connection shortcuts manually by typing ssh://myuser@myhost in TextEdit, highlighting that text and dragging it to an indexed folder, that automagically creates a webloc (inetloc) file. The advantage is, I can define which username is used. I am a system administrator and need to logon as root on 99% of my machines, not as the user I am on my MacBook.

But there's of course an easy way to do that with your script as well: Just replace
Code: Select all
<string>ssh://#{hostname}</string>

with
Code: Select all
<string>ssh://root@#{hostname}</string>

and all the automagically created ssh connection profiles will use the root user.

In the rare case that I don't have root access to a server and thus need to log on with another username, I can always alter the created webloc file afterwards.

This script is definitely the best addition for Launchbar for me. Thanks :D
Jay2k1
 
Posts: 1
Joined: Thu Nov 05, 2009 10:40 am

Re: Add known/configured SSH hosts to index

Postby antifuchs » Sat Jul 20, 2013 1:56 am

Just a small update to this: I've just released the code I use to generate these bookmarks (which I've apparently used for ~4 years now!) as a ruby gem. You can find install instructions at https://github.com/antifuchs/ssh_bookmarker.
antifuchs
 
Posts: 3
Joined: Mon Aug 03, 2009 12:24 pm


Return to LaunchBar Actions

Who is online

Users browsing this forum: No registered users and 1 guest

cron