fix client_focused()
[wmiirc-lua.git] / plugins / ssh.lua
blob549f16ab15ed990391a1e0a0ae25bdd75c4582ed
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 wmii.set_conf ("ssh.askforuser", true);
52 local hosts
53 local users
55 function load_hosts()
56 hosts = {}
58 local file = io.open(os.getenv("HOME") .. "/.ssh/known_hosts", "r")
59 if file then
60 local line = file:read("*line")
62 while line do
63 local host = line:match("([^ ,]+)")
64 hosts[host] = 1
65 line = file:read("*line")
66 end
67 file:close()
68 end
69 end
71 function load_users()
72 users = {}
74 local file = io.open("/etc/passwd", "r")
75 if file then
76 local line = file:read("*line")
78 users[""] = 1
79 while line do
80 local user = line:match("([^:]+)")
81 users[user] = 1
82 line = file:read("*line")
83 end
84 file:close()
85 end
86 end
88 function show_menu()
89 local str = wmii.menu(hosts, "ssh:")
90 if type(str) == "string" then
91 local cmd = wmii.get_conf("xterm") .. " -e /bin/sh -c \"exec ssh "
92 if wmii.get_conf("ssh.askforuser") then
93 local user = wmii.menu(users, "username:")
94 if type(user) == "string" and user ~= "" then
95 cmd = cmd .. "-l " .. user .. " "
96 end
97 end
98 cmd = cmd .. str .. "\" &"
99 os.execute(cmd)
103 load_hosts()
104 load_users()