SSH menu for easy SSHing to known hosts
[wmiirc-lua.git] / plugins / ssh.lua
blob69118b5435af671b2d4e64dd217cebdb85af4a8d
1 --[[
2 =pod
4 =head1 NAME
6 ssh.lua - SSH menu for known hosts.
8 =head1 SYNOPSIS
10 Add something like:
12 wmii.load_plugin ("ssh")
13 wmii.add_key_handler ("Mod1-z", ssh.show_menu)
15 into your wmiirc.lua.
17 =head1 DESCRIPTION
19 This reads ~/.ssh/known_hosts in order to display a menu of hosts (and IP
20 addresses) found in the file. It assumes 'HashKnownHosts no' is set in
21 ~/.ssh/config (otherwise it displays the hashed hosts).
23 =head1 SEE ALSO
25 L<wmii(1)>, L<lua(1)>
27 =head1 AUTHOR
29 David Leadbeater <dgl@dgl.cx>
31 =head1 LICENCE AND COPYRIGHT
33 Copyright (c) 2008, David Leadbeater <dgl@dgl.cx>
35 This is free software. You may redistribute copies of it under the terms of
36 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
37 is NO WARRANTY, to the extent permitted by law.
39 =cut
40 --]]
42 local wmii = require("wmii")
43 local os = require("os")
44 local io = require("io")
45 local type = type
47 module ("ssh")
48 api_version=0.1
50 local hosts
52 function load_hosts()
53 hosts = {}
55 local file = io.open(os.getenv("HOME") .. "/.ssh/known_hosts", "r")
56 if file then
57 local line = file:read("*line")
59 while line do
60 local host = line:match("([^ ,]+)")
61 hosts[host] = 1
62 line = file:read("*line")
63 end
64 file:close()
65 end
66 end
68 function show_menu()
69 local str = wmii.menu(hosts, "ssh:")
70 if type(str) == "string" then
71 local cmd = wmii.get_conf("xterm") .. " -e ssh " .. str .. " &"
72 os.execute(cmd)
73 end
74 end
76 load_hosts()