SSH: prompt

From awesome

Jump to: navigation, search

For Awesome 3.4, it goes like this. Using modkey + F2 will give you an SSH prompt, where you can type hostname or user@hostname and will get an urxvt terminal (replace urxvt with your favourite terminal program) connected as user@hostname. You need to add this in the Prompt section of the config file.

    pure_terminal = "urxvt"
    awful.key({ modkey },       "F2",
            function ()
                    awful.prompt.run({ prompt = "SSH: " },
                    mypromptbox[mouse.screen].widget,
                    function (s)
                            awful.util.spawn(pure_terminal .. " -e ssh " .. s)
                    end)
            end),


Add this line to your rc.lua. After pressing modMask+s, prompt appears, type in the host you want to log to (even user@host works) and there you go. ;]

keybinding({ modMask }, "s", function () awful.prompt.run({ prompt = "SSH: " }, 
                                                            promptbox[mouse.screen],
                                                            function (s) awful.util.spawn("exec xterm -e ssh -v " .. s) end)
                             end):add()


[edit] Hostname completion (by lwi)

probably not perfect at the moment ;) e.g. there is no user completion atm, just hostname completion

terminal = "urxvtc"
keybinding({ modkey }, "s", function ()
    awful.prompt.run({ prompt = "ssh to: " },
    mypromptbox[mouse.screen],
    function(h)
        awful.util.spawn(terminal .. " -e ssh " .. h)
    end,
    function(cmd, cur_pos, ncomp)
        local hosts, matches, filehandle = {}, {}, io.open(os.getenv("HOME") .. "/.ssh/config")
        for host in filehandle:read("*all"):gmatch("Host ([%w%p]+)") do
            table.insert(hosts, host)
        end     
        filehandle:close()
        -- abort completion under certain circumstances
        if #cmd == 0 or (cur_pos ~= #cmd + 1 and cmd:sub(cur_pos, cur_pos) ~= " ") then
            return cmd, cur_pos
        end     
        -- match
        for i, host in pairs(hosts) do
            if host:find("^" .. cmd:sub(1,cur_pos)) then
                table.insert(matches, host)
            end     
        end       
        -- if there are no matches
        if #matches == 0 then
            return cmd, cur_pos
        end 
        -- cycle
        while ncomp > #matches do
            ncomp = ncomp - #matches
        end     
        -- return match and position
        return matches[ncomp], cur_pos
    end,
    awful.util.getdir("cache") .. "/ssh_history")
end):add()

[edit] Hostname completion, based on ~/.ssh/known_hosts file and lwi script

You need to disable "HashKnownHosts" in your ssh config to have the following working. You can do this by editing your ~/.ssh/config

Host *
       HashKnownHosts no

Lua code :

terminal = "urxvtc"
keybinding({ modkey }, "F2", function ()
        awful.prompt.run({ prompt = "SSH TO: " },
        mypromptbox[mouse.screen],
        function(h)
                awful.util.spawn(terminal .. " -e \"ssh -v " .. h .. "\"")
        end,
        function(cmd, cur_pos, ncomp)
                -- get the hosts
                local hosts = {}
                tmp_file = "/tmp/ssh." .. os.time()
                os.execute('cut -d " " -f1 ' .. os.getenv("HOME") .. '/.ssh/known_hosts | cut -d, -f1 > ' .. tmp_file)
                f = io.open(tmp_file)
                for host in f:lines() do
                        table.insert(hosts, host)
                end
                f:close()
                os.execute('rm ' .. tmp_file)
                -- abort completion under certain circumstances
                if #cmd == 0 or (cur_pos ~= #cmd + 1 and cmd:sub(cur_pos, cur_pos) ~= " ") then
                        return cmd, cur_pos
                end
                -- match
                local matches = {}
                table.foreach(hosts, function(x)
                        if hosts[x]:find("^" .. cmd:sub(1,cur_pos)) then
                                table.insert(matches, hosts[x])
                        end
                end)
                -- if there are no matches
                if #matches == 0 then
                        return
                end
                -- cycle
                while ncomp > #matches do
                        ncomp = ncomp - #matches
                end
                -- return match and position
                return matches[ncomp], cur_pos
        end,
        awful.util.getdir("cache") .. "/ssh_history")
end):add()

[edit] Same as above, but working in awesome3 and without a temp file

    awful.key({ modkey,           }, "F2", function ()
        awful.prompt.run({ prompt = "ssh: " },
        mypromptbox[mouse.screen].widget,
        function(h)
                awful.util.spawn(terminal .. " -e ssh " .. h)
        end,
        function(cmd, cur_pos, ncomp)
                -- get the hosts
                local hosts = {}
                f = io.popen('cut -d " " -f1 ' .. os.getenv("HOME") ..  '/.ssh/known_hosts | cut -d, -f1')
                for host in f:lines() do
                        table.insert(hosts, host)
                end
                f:close()
                -- abort completion under certain circumstances
                if #cmd == 0 or (cur_pos ~= #cmd + 1 and cmd:sub(cur_pos, cur_pos) ~= " ") then
                        return cmd, cur_pos
                end
                -- match
                local matches = {}
                table.foreach(hosts, function(x)
                        if hosts[x]:find("^" .. cmd:sub(1,cur_pos)) then
                                table.insert(matches, hosts[x])
                        end
                end)
                -- if there are no matches
                if #matches == 0 then
                        return
                end
                -- cycle
                while ncomp > #matches do
                        ncomp = ncomp - #matches
                end
                -- return match and position
                return matches[ncomp], cur_pos
        end,
        awful.util.getdir("cache") .. "/ssh_history")
end),
Personal tools