kupferlauncher / kupfer

kupfer, smart, quick launcher. `master' is kupfer's release branch.
http://kupferlauncher.github.io
GNU General Public License v3.0
368 stars 64 forks source link

Plugin ssh-host: implement copy ip to clipboard action ? #169

Closed khurshid-alam closed 11 months ago

khurshid-alam commented 1 year ago

My ssh hosts includes something like, (note, it has aliases for small letter since gvfs doesn't support capital letter in Host).

 Host Jammy-Dev jammy-dev
    User vncuser
    HostName 95.219.x.x
    Port 22
    ProxyCommand ssh -W %h:%p SecureServer

Sometimes I need quickly need the ip address of my server. My attempt is so far is a hack to find ip with command-line tool,

class CopyIP (Action):
    # rank down since it applies everywhere
    #rank_adjust = -2
    action_accelerator = "c"
    def __init__(self):
        Action.__init__(self, _("Copy Host IP Address"))

    def activate(self, leaf):
        cmd = "ssh -G " + leaf[HOST_ADDRESS_KEY] + " | grep -E '^hostname' | awk -F ' '  '{{print $2}}' | tr -d '\n'"
        proc = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=None)
        ip_addr = (proc.communicate()[0]).decode("utf-8")
        clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        clip.set_text(ip_addr, -1)

    def item_types(self):
        yield HostLeaf

    def valid_for_item(self, item):
        if item.check_key(HOST_SERVICE_NAME_KEY):
            return item[HOST_SERVICE_NAME_KEY] == 'ssh'
        return False

    def get_description(self):
        return _("Copy Host IP Address")

    def get_icon_name(self):
        return "edit-paste"

However this doesn't have to be the case. Host object already contains,

HOST_NAME_KEY = "HOST_NAME"
HOST_ADDRESS_KEY = "HOST_ADDRESS"

and so far the plugin is using name for both in SSHSource. So it can pull and while ssh-connect can use leaf[HOST_NAME_KEY], copy-ip can use leaf[HOST_ADDRESS_KEY]

(There will be some edge cases which needs to be handled.)

Edit: I saw the new beta, I haven't tested this. but perhaps it better to implement in beta.

Environment

Kupfer Version: V320 Window Manager: Unity/Compiz Desktop Environment: Unity Linux Distribution: Ubuntu 22.04

@KarolBedkowski Thoughts ?

KarolBedkowski commented 1 year ago

I think this may be useful as long this action will be global - for all HostLeaves, not only for ssh. Of course SSHSource need modification and maybe other sources.

I'll look at it in next free moment.