Jump to content


Photo

SSH with smart hostname autocompletion

workflow ssh fuzzy python back to my mac bonjour secure shell

  • Please log in to reply
44 replies to this topic

#1 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 13 March 2013 - 06:46 PM

This one has been at the top of my Alfred wishlist since I downloaded my first Alfred v0.whatever beta: a workflow to open an ssh session (using the default ssh: protocol handler) with "smart" hostname autocompletion (based on ~/.ssh/config, ~/.ssh/known_hosts, /etc/hosts and Bonjour).

 

NB: for Bonjour discovery to work, you'll need to install the pybonjour module, e.g. `sudo /usr/bin/easy_install pybonjour`. 

 

Comments and suggestions welcome.

 

Download (v1.1)

 

Source on github

 

Robin

 

Updates

2013-03-14 00:00, v0.4: rudimentary fuzzy matching, to pull entries from /etc/hosts as well as ~/.ssh/known_hosts, and to deal with usernames properly.

2013-03-14 09:00, v0.5: smart host matching.

2013-03-14 22:45, v0.6: ~/.ssh/config and Bonjour discovery (including full BTMM support), and improved error handling.

2013-03-14 23:00, v0.61: explicitly removed 'broadcasthost' from the list of possible matches.

2013-03-15 00:30, v0.7: match '.' as '.'

2013-03-16 10:45, v0.8: refactored code and add tab autocompletion.

2013-03-16 13:30, v0.9: oops - the last update broke the workflow if you didn't have pybonjour installed; fixed. Continued refactoring: now using nikipore's alfred-python module.

2013-03-16 14:15, v0.10: further refactoring to break core functionality out into a dedicated module (enabling module compilation). Combined with caching of file parsing results this should deliver significantly enhanced performance (not that it was slow before).

2013-03-16 23:00, v1.0: complete refactoring and move release to github. New feature: display the source(s) of each suggested completion. This ought to be the last update for a while.

2013-03-16 08:40, v1.1: fixed breakage when either of ~/.ssh/config or ~/.ssh/known_hosts didn't exist.


Edited by isometry, 11 July 2013 - 06:02 PM.

  • phyllisstein, Enhorn, Sebastian and 1 other like this

#2 phyllisstein

phyllisstein

    Advanced Member

  • Member
  • PipPipPip
  • 368 posts
  • LocationNew York, NY

Posted 14 March 2013 - 03:06 PM

Great work! Thanks so much for sharing it.


alp, Alfred.framework, and my assorted workflows are now abandonware. You can find them at https://github.com/p...stein/Workflows.

 


#3 Benton Barnett

Benton Barnett

    Newbie

  • Member
  • Pip
  • 1 posts

Posted 14 March 2013 - 08:18 PM

This is awesome! Exactly what I was looking for when I bought the powerpack :)



#4 Bas

Bas

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 14 March 2013 - 08:38 PM

Cool!
 
How do i make this work with iTerm2? The flow only opens Terminal.
 
Regards,
Bas


#5 ctwise

ctwise

    Member

  • Member
  • PipPip
  • 204 posts

Posted 14 March 2013 - 08:43 PM

Cool!
 
How do i make this work with iTerm2? The flow only opens Terminal.
 
Regards,
Bas

 

http://apple.stackex...ssh-url-handler


  • Bas and firesofmay like this

#6 Bas

Bas

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 14 March 2013 - 08:49 PM

Thank you for your quick reply. Works perfectly!


#7 attack7

attack7

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 14 March 2013 - 09:18 PM

This is wonderful.  I wonder if there could also be a way to specify a user... for example if I had an IP ending in .123 to which I want to connect, if I could type

 

ssh another-user 123  to generate   ssh another-user@192.122.122.123   or ssh 192.122.122.123 -l another-user ?



#8 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 14 March 2013 - 09:19 PM

http://apple.stackex...ssh-url-handler

 

For your iTerm2 ssh: protocol handler profile, you can set the Name to $$HOST$$ and the Command to $$ to have it automatically set the window/tab title to the name of the host you're connecting to (including username, if specified) and save it spawning a copy of your default shell to simply run ssh.

 

 

Update 2013-08-12: Changed the recommended Command to simply $$ from ssh $$HOST$$. The latter incorrectly threw away the username if specified.


Edited by isometry, 12 August 2013 - 10:21 PM.


#9 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 14 March 2013 - 09:20 PM

This is wonderful.  I wonder if there could also be a way to specify a user... for example if I had an IP ending in .123 to which I want to connect, if I could type

 

ssh another-user 123  to generate   ssh another-user@192.122.122.123   or ssh 192.122.122.123 -l another-user ?

 

Simply include the username in your host specification: it does "the right thing". I.e. `ssh user@host` or `ssh user@123`



#10 attack7

attack7

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 14 March 2013 - 09:29 PM

Simply include the username in your host specification: it does "the right thing". I.e. `ssh user@host` or `ssh user@123`

Works like a champ - can't believe I didn't just try it...  Thanks!



#11 cmsj

cmsj

    Newbie

  • Member
  • Pip
  • 10 posts

Posted 14 March 2013 - 09:31 PM

I added support for reading user-defined hosts from ~/.ssh/config: http://paste.ubuntu.com/5614867/

 

(can't believe I didn't think to write this workflow, thanks very much!)



#12 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 14 March 2013 - 09:37 PM

I added support for reading user-defined hosts from ~/.ssh/config: http://paste.ubuntu.com/5614867/

 

(can't believe I didn't think to write this workflow, thanks very much!)

 

I had considered adding parsing of .ssh/config, but mine contains a lot of wildcards and other such cruft. Once you've connected to a host once, it's in your .ssh/known_hosts and good-to-go evermore. Does this addition make your workflow noticeably better?

 

NB: I've already added Bonjour autocompletion to my development version, including full Back to My Mac support. Will post soon.



#13 cmsj

cmsj

    Newbie

  • Member
  • Pip
  • 10 posts

Posted 14 March 2013 - 09:52 PM

I had considered adding parsing of .ssh/config, but mine contains a lot of wildcards and other such cruft. Once you've connected to a host once, it's in your .ssh/known_hosts and good-to-go evermore. Does this addition make your workflow noticeably better?

 

NB: I've already added Bonjour autocompletion to my development version, including full Back to My Mac support. Will post soon.

 

Good point about the wildcards, my patch is not doing enough to exclude them. Should be sufficient to skip any line with * or ? in it.

 

For me the killer feature of entries in the ssh config is that I can fake the name to combine various settings and then set the real host with the Hostname option. In that situation, the Host does not end up in known_hosts.

 

Love the sound of Bonjour and BTMM support :)



#14 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 14 March 2013 - 10:47 PM

Good point about the wildcards, my patch is not doing enough to exclude them. Should be sufficient to skip any line with * or ? in it.

 

For me the killer feature of entries in the ssh config is that I can fake the name to combine various settings and then set the real host with the Hostname option. In that situation, the Host does not end up in known_hosts.

 

Love the sound of Bonjour and BTMM support :)

 

I like it.

 

I've just posted v0.6 which includes a variant of your patch (deals with all the ssh_config pattern characters) and Bonjour discovery, along with much better error handling (e.g. complete failure if you didn't actually have a ~/.ssh/known_hosts or ~/.ssh/config file). Enjoy!



#15 cmsj

cmsj

    Newbie

  • Member
  • Pip
  • 10 posts

Posted 14 March 2013 - 10:51 PM

I like it.

 

I've just posted v0.6 which includes a variant of your patch (deals with all the ssh_config pattern characters) and Bonjour discovery, along with much better error handling (e.g. complete failure if you didn't actually have a ~/.ssh/known_hosts or ~/.ssh/config file). Enjoy!

 

Very nice. I noticed that "broadcasthost" shows up if I happen to type any of its characters. I'd not come across this before in OSX, but it appears to be an alias to 255.255.255.255, so is probably worth filtering out at some point :)



#16 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 14 March 2013 - 11:01 PM

Very nice. I noticed that "broadcasthost" shows up if I happen to type any of its characters. I'd not come across this before in OSX, but it appears to be an alias to 255.255.255.255, so is probably worth filtering out at some point :)

 

Indeed. I was trying to avoid the need to special case, but it is ugly… I've updated to v0.61.



#17 nikipore

nikipore

    Member

  • Member
  • PipPip
  • 40 posts

Posted 15 March 2013 - 08:51 AM

Cool! I've got a suggestion: When you're a heavy SSH user, it may become difficult to tell local Terminal windows and the various remote Terminal windows apart because they all have the same color theme. Plus, one sometimes wants to tunnel a port or three, or the target port isn't 22. For that reason, I establish my SSH sessions via .terminal files which I can also find via Alfred. These are basically .plist files with another file extension (this one I use to tunnel VNC access):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>BackgroundColor</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGFRZYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKMHCA9VJG51bGzTCQoLDA0OViRjbGFzc1xOU0NvbG9yU3BhY2VVTlNSR0KAAhAB
	RjAgMCAwANIQERITWiRjbGFzc25hbWVYJGNsYXNzZXNXTlNDb2xvcqISFFhOU09iamVj
	dF8QD05TS2V5ZWRBcmNoaXZlctEXGFRyb290gAEIERojLTI3O0FIT1xiZGZtcn2GjpGa
	rK+0AAAAAAAAAQEAAAAAAAAAGQAAAAAAAAAAAAAAAAAAALY=
	</data>
	<key>CommandString</key>
        <string>ssh me@somewhere.net -L5900:localhost:5900</string> 
	<key>CursorBlink</key>
	<true/>
	<key>CursorColor</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGICFYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKYHCBMUFRpVJG51bGzVCQoLDA0ODxAREl1OU0NhdGFsb2dOYW1lViRjbGFzc1tO
	U0NvbG9yTmFtZVxOU0NvbG9yU3BhY2VXTlNDb2xvcoACgAWAAxAGgARWU3lzdGVtXxAY
	ZGlzYWJsZWRDb250cm9sVGV4dENvbG9y0woMFg8YGVdOU1doaXRlgAUQA00wLjMzMzMz
	MzMzMzMA0hscHR5aJGNsYXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yoh0fWE5TT2JqZWN0
	XxAPTlNLZXllZEFyY2hpdmVy0SIjVHJvb3SAAQAIABEAGgAjAC0AMgA3AD4ARABPAF0A
	ZABwAH0AhQCHAIkAiwCNAI8AlgCxALgAwADCAMQA0gDXAOIA6wDzAPYA/wERARQBGQAA
	AAAAAAIBAAAAAAAAACQAAAAAAAAAAAAAAAAAAAEb
	</data>
	<key>CursorType</key>
	<integer>1</integer>
	<key>Font</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGGBlYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKQHCBESVSRudWxs1AkKCwwNDg8QViRjbGFzc1ZOU05hbWVWTlNTaXplWE5TZkZs
	YWdzgAOAAiNAKAAAAAAAABAQXU1lbmxvLVJlZ3VsYXLSExQVFlokY2xhc3NuYW1lWCRj
	bGFzc2VzVk5TRm9udKIVF1hOU09iamVjdF8QD05TS2V5ZWRBcmNoaXZlctEaG1Ryb290
	gAEIERojLTI3PEJLUllgaWttdniGi5afpqmyxMfMAAAAAAAAAQEAAAAAAAAAHAAAAAAA
	AAAAAAAAAAAAAM4=
	</data>
	<key>FontAntialias</key>
	<true/>
	<key>FontWidthSpacing</key>
	<real>1.004032258064516</real>
	<key>ProfileCurrentVersion</key>
	<real>2.0099999999999998</real>
	<key>RunCommandAsShell</key>
	<true/>
	<key>SelectionColor</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGICFYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKYHCBMUFRpVJG51bGzVCQoLDA0ODxAREl1OU0NhdGFsb2dOYW1lViRjbGFzc1tO
	U0NvbG9yTmFtZVxOU0NvbG9yU3BhY2VXTlNDb2xvcoACgAWAAxAGgARWU3lzdGVtXxAY
	ZGlzYWJsZWRDb250cm9sVGV4dENvbG9y0woMFg8YGVdOU1doaXRlgAUQA00wLjMzMzMz
	MzMzMzMA0hscHR5aJGNsYXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yoh0fWE5TT2JqZWN0
	XxAPTlNLZXllZEFyY2hpdmVy0SIjVHJvb3SAAQAIABEAGgAjAC0AMgA3AD4ARABPAF0A
	ZABwAH0AhQCHAIkAiwCNAI8AlgCxALgAwADCAMQA0gDXAOIA6wDzAPYA/wERARQBGQAA
	AAAAAAIBAAAAAAAAACQAAAAAAAAAAAAAAAAAAAEb
	</data>
	<key>ShowActiveProcessInTitle</key>
	<false/>
	<key>ShowCommandKeyInTitle</key>
	<false/>
	<key>ShowShellCommandInTitle</key>
	<false/>
	<key>ShowTTYNameInTitle</key>
	<false/>
	<key>ShowWindowSettingsNameInTitle</key>
	<true/>
	<key>TextBoldColor</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGICFYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKYHCBMUFRpVJG51bGzVCQoLDA0ODxAREl1OU0NhdGFsb2dOYW1lViRjbGFzc1tO
	U0NvbG9yTmFtZVxOU0NvbG9yU3BhY2VXTlNDb2xvcoACgAWAAxAGgARWU3lzdGVtXxAa
	Y29udHJvbExpZ2h0SGlnaGxpZ2h0Q29sb3LTCgwWDxgZV05TV2hpdGWABRADQjEA0hsc
	HR5aJGNsYXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yoh0fWE5TT2JqZWN0XxAPTlNLZXll
	ZEFyY2hpdmVy0SIjVHJvb3SAAQAIABEAGgAjAC0AMgA3AD4ARABPAF0AZABwAH0AhQCH
	AIkAiwCNAI8AlgCzALoAwgDEAMYAyQDOANkA4gDqAO0A9gEIAQsBEAAAAAAAAAIBAAAA
	AAAAACQAAAAAAAAAAAAAAAAAAAES
	</data>
	<key>TextColor</key>
	<data>
	YnBsaXN0MDDUAQIDBAUGICFYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
	AAGGoKYHCBMUFRpVJG51bGzVCQoLDA0ODxAREl1OU0NhdGFsb2dOYW1lViRjbGFzc1tO
	U0NvbG9yTmFtZVxOU0NvbG9yU3BhY2VXTlNDb2xvcoACgAWAAxAGgARWU3lzdGVtWWdy
	aWRDb2xvctMKDBYPGBlXTlNXaGl0ZYAFEANEMC41ANIbHB0eWiRjbGFzc25hbWVYJGNs
	YXNzZXNXTlNDb2xvcqIdH1hOU09iamVjdF8QD05TS2V5ZWRBcmNoaXZlctEiI1Ryb290
	gAEACAARABoAIwAtADIANwA+AEQATwBdAGQAcAB9AIUAhwCJAIsAjQCPAJYAoACnAK8A
	sQCzALgAvQDIANEA2QDcAOUA9wD6AP8AAAAAAAACAQAAAAAAAAAkAAAAAAAAAAAAAAAA
	AAABAQ==
	</data>
	<key>UseBoldFonts</key>
	<false/>
	<key>UseBrightBold</key>
	<true/>
	<key>columnCount</key>
	<integer>140</integer>
	<key>name</key>
	<string>Terminal Settings</string>
	<key>rowCount</key>
	<integer>40</integer>
	<key>shellExitAction</key>
	<integer>0</integer>
	<key>type</key>
	<string>Window Settings</string>
</dict>
</plist>

So what I'm suggesting is the following: Why not put a default.terminal template in the workflow folder having a tag

 

<key>CommandString</key>
<string>ssh %(address)s</string>

 

Then write a copy of this file for each user and host to the non-volatile work folder and pass the file name to Alfred? Then, a user has various places to tweak the settings per connection.

 

I can also implement that if you like.



#18 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 15 March 2013 - 07:05 PM

nikipore,

 

Your suggestion has its merits, though as described I believe it would be best implemented in a different workflow. This one attempts to be independent of the user's chosen terminal emulator by exploiting the ssh: protocol handler. As such, it supports not just Terminal.app but also iTerm.app, iTerm2.app and any others I don't have personal familiarity with.

 

Having said that, I do like the idea of a workflow to support management of background ssh tunnels, so I may look at that :-)


Robin



#19 DavidK

DavidK

    Member

  • Member
  • PipPip
  • 32 posts

Posted 15 March 2013 - 11:51 PM

FYI - I'm geting the following error when trying to download from your droplr link.

 

 

 

AccessDeniedAccess denied
 


#20 isometry

isometry

    Member

  • Member
  • PipPip
  • 36 posts

Posted 16 March 2013 - 10:49 AM

FYI - I'm geting the following error when trying to download from your droplr link.

AccessDeniedAccess denied

 

Peculiar - it's reporting 190 downloads of the latest version. Anyway, I've just updated to v0.8 with a refactoring of the code and the addition of tab autocompletion.





Also tagged with one or more of these keywords: workflow, ssh, fuzzy, python, back to my mac, bonjour, secure shell

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users