Search code examples
macossshapplescriptosx-snow-leoparditerm

Using ssh handler links to paste into existing iterm tab


I use an ssh gateway, which uses key-auth. This means my work station is not keyed into any of the servers I work with daily. I use ssh:// handlers, which I had a script for on my PC to work with putty (click link, then click open putty window to paste). I wanted to do something similar with applescript, but have been unable to even get rudimentary scripts to work.

Is there an easier way? I really just need to be able to convert "ssh://root@ip:port" to "ssh root@ip -p port" (ex.) and paste it into the active iterm2 tab. If there is a more elegant way to do this I am open to suggestions. I have found dozens of pages that skip around this idea, but I really can't wrap my head around the applescript they use (nor can I seem to reproduce it).

edit for clarification:

I always have an ssh session to the gateway open in iterm, which is where i keep multiple screens of various things I am doing. Ideally I just want to throw this into my clipboard so I can just tab to my gateway and cmd+v. This is easier than interacting with iterm, however I can't wrap my head around the applescript for parsing the argument line. Additionally I have modified info.plist to accept "ssh" as the scheme, but would something else be needed?

=---------------------=

This is as far as I have gotten after continuing my search, and while it mostly works in CLI during testing (open script.app/ ssh://[email protected]:22), It does not work with my browser. Frankly, I don't really know what I am doing.

on open location this_URL

    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"ssh://", ":"}
    set preBuff to item 2 of the text items of this_URL
    set prePort to item 3 of the text items of this_URL as integer
    set preLine to "ssh " & prebuf & " -p " & prePort


    set the clipboard to preLine


end open location

=---------------------=

I have worked on this a bit more, and:

on open location this_URL

    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"ssh://", ":"}
    set preBuff to item 2 of the text items of this_URL
    set prePort to item 3 of the text items of this_URL as integer
    set preLine to "ssh " & preBuf & " -p " & prePort

    -- debug dialog
    display dialog the [preLine]
    buttons("exit")

    set the clipboard to preLine


end open location
-- debug dialog
display dialog the ["didn't run"]
buttons("exit")

The "didn't run" dialog will pop up when a ssh:// link is clicked in the browser, however everything enclosed in "on open location" doesn't appear to be touched.

EDIT: My info.plist - http://pastebin.com/JgRps1B5


Solution

  • I have actually gotten this to work now, using this:

    on open location test
        if test contains "rm" then --shitty sanitization
            display dialog the ["Link may be malicious"] --debug
        else
            set oldDelims to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"ssh://", ":", "/"}
            set preHost to item 2 of the text items of test
            set prePort to item 3 of the text items of test as integer
            set final to "ssh " & preHost & " -p " & prePort
            set the clipboard to final
            display dialog the [final] --debug
        end if
    end open location
    

    I don't know why one worked and the other didn't, I can only guess there was some error it was not passing. The end result:

    ssh://[email protected]:22/ (ex) gets placed in the clipboard as "ssh [email protected] -p 22"

    So for the most part, this is solved (I just don't know why).