[PATCH] SSH menu for easy SSHing to known hosts

12 views
Skip to first unread message

David Leadbeater

unread,
Oct 23, 2008, 10:27:38 AM10/23/08
to wmii...@googlegroups.com
Note this reads ~/.ssh/known_hosts and assumes 'HashKnownHosts no' is set in
ssh config.
---
plugins/ssh.lua | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 76 insertions(+), 0 deletions(-)
create mode 100644 plugins/ssh.lua

diff --git a/plugins/ssh.lua b/plugins/ssh.lua
new file mode 100644
index 0000000..69118b5
--- /dev/null
+++ b/plugins/ssh.lua
@@ -0,0 +1,76 @@
+--[[
+=pod
+
+=head1 NAME
+
+ssh.lua - SSH menu for known hosts.
+
+=head1 SYNOPSIS
+
+Add something like:
+
+ wmii.load_plugin ("ssh")
+ wmii.add_key_handler ("Mod1-z", ssh.show_menu)
+
+into your wmiirc.lua.
+
+=head1 DESCRIPTION
+
+This reads ~/.ssh/known_hosts in order to display a menu of hosts (and IP
+addresses) found in the file. It assumes 'HashKnownHosts no' is set in
+~/.ssh/config (otherwise it displays the hashed hosts).
+
+=head1 SEE ALSO
+
+L<wmii(1)>, L<lua(1)>
+
+=head1 AUTHOR
+
+David Leadbeater <d...@dgl.cx>
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright (c) 2008, David Leadbeater <d...@dgl.cx>
+
+This is free software. You may redistribute copies of it under the terms of
+the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
+is NO WARRANTY, to the extent permitted by law.
+
+=cut
+--]]
+
+local wmii = require("wmii")
+local os = require("os")
+local io = require("io")
+local type = type
+
+module ("ssh")
+api_version=0.1
+
+local hosts
+
+function load_hosts()
+ hosts = {}
+
+ local file = io.open(os.getenv("HOME") .. "/.ssh/known_hosts", "r")
+ if file then
+ local line = file:read("*line")
+
+ while line do
+ local host = line:match("([^ ,]+)")
+ hosts[host] = 1
+ line = file:read("*line")
+ end
+ file:close()
+ end
+end
+
+function show_menu()
+ local str = wmii.menu(hosts, "ssh:")
+ if type(str) == "string" then
+ local cmd = wmii.get_conf("xterm") .. " -e ssh " .. str .. " &"
+ os.execute(cmd)
+ end
+end
+
+load_hosts()
--
1.5.4.1.34.g94bf-dirty

Bart Trojanowski

unread,
Oct 23, 2008, 9:22:50 PM10/23/08
to wmii...@googlegroups.com, Jan-David Quesel, que...@gmail.com
* David Leadbeater <d...@dgl.cx> [081023 10:33]:

> +Add something like:
> +
> + wmii.load_plugin ("ssh")
> + wmii.add_key_handler ("Mod1-z", ssh.show_menu)
> +
> +into your wmiirc.lua.

Awesome, thanks.

> +This reads ~/.ssh/known_hosts in order to display a menu of hosts (and IP
> +addresses) found in the file. It assumes 'HashKnownHosts no' is set in
> +~/.ssh/config (otherwise it displays the hashed hosts).

Would it also make sense to read in .ssh/config 'Host' lines?

> + while line do
> + local host = line:match("([^ ,]+)")

Please ignore lines that start with '|' because those are hashed
entries... is that right?

Also my known_hosts includes lines like:

foo.bar.com,[10.10.10.10]:2222

For extra credit, it would be nice to ssh with that port number :)

* que...@gmail.com <que...@gmail.com> [081023 11:16]:
> +function load_users()
> + users = {}
> +
> + local file = io.open("/etc/passwd", "r")


> + if file then
> + local line = file:read("*line")
> +

> + users[""] = 1
> + while line do
> + local user = line:match("([^:]+)")
> + users[user] = 1

I wonder, is there an easy way to skip non-user users? Having 'bin' and
'sys' in there is just clutter.

* que...@gmail.com <que...@gmail.com> [081023 12:20]:
> -wmii.set_conf ("ssh.askforuser", "true");
> +wmii.set_conf ("ssh.askforuser", true);

This breaks the script. Perhaps it was poor judgement on my part but
set_conf currently only takes a string or number. Anyone object to
this...

diff --git a/core/wmii.lua b/core/wmii.lua
index ee00e4b..d7bb49b 100644
--- a/core/wmii.lua
+++ b/core/wmii.lua
@@ -1395,7 +1395,8 @@ function set_conf (first,second)

elseif type(first) == "string"
and (type(second) == "string"
- or type(second) == "number") then
+ or type(second) == "number"
+ or type(second) == "boolean") then
config[first] = second

Anyway, thanks guys.

Can I get both of you to Sign-off on the final patch?

Cheers,
-Bart

--
WebSig: http://www.jukie.net/~bart/sig/

Dave O'Neill

unread,
Oct 23, 2008, 9:29:55 PM10/23/08
to wmii...@googlegroups.com, Jan-David Quesel, que...@gmail.com
Bart Trojanowski wrote:

> This breaks the script. Perhaps it was poor judgement on my part but
> set_conf currently only takes a string or number. Anyone object to
> this...

[ snip ]

No objections to adding booleans to set_conf.

Cheers,
Dave

Dave O'Neill

unread,
Oct 23, 2008, 9:33:32 PM10/23/08
to wmii...@googlegroups.com
Bart Trojanowski wrote:
>
> I wonder, is there an easy way to skip non-user users? Having 'bin' and
> 'sys' in there is just clutter.

On most Linux systems, "real" users and system users are grouped with
different UIDs. Unfortunately, that's not standard. On Debian, the
convention is that system users are supposed to have UIDs below 1000.
On Red Hat and Fedora, it's supposed to be below 500.

Cheers,
Dave

Dave O'Neill

unread,
Oct 23, 2008, 9:45:41 PM10/23/08
to wmii...@googlegroups.com

And of course, I think of the solution right after I send the mail. Add
a config option:

wmii.set_conf("ssh.ignore_below_uid", 500)

and ignore any passwd entry that has a UID below that option's value.
Users on Debian can bump the config value to 1000 if necessary.

Cheers,
Dave

Jan-David Quesel

unread,
Oct 24, 2008, 9:30:55 AM10/24/08
to wmii...@googlegroups.com
Hi,

me neither ;)

Regards,
Jan

--
Jan-David Quesel
Carl von Ossietzky Universität Oldenburg
Department of Computing Science
Correct System Design
D-26111 Oldenburg, Germany

Phone: +49 (0)441 798-2376
Fax: +49 (0)441 798-2965

Reply all
Reply to author
Forward
0 new messages