News for hackers. Stuff that matters.

Ask HN: Why is there no native SSH hook to run a local command before connecting

The `LocalCommand` parameter only runs a command on the local machine after the connection is established.

Is there no pre-connection hook?

My use case is simply that I do port knocking to expose my ssh port, and want this working automatically at the SSH config level so that I can use it transparently from other utilities (e.g. Ansible inventory, Emacs over TRAMP)

( 13 comments | 2 points | on HN )
Re:Ask HN: Why is there no native SSH hook to run a local co... by Bender on Sunday September 07, 2025 @06:22PM (#45162726)
Re:Ask HN: Why is there no native SSH hook to run a local co... by ranger_danger on Tuesday September 09, 2025 @07:20PM (#45190856)
Besides the ProxyCommand approach, another way would be to define 'ssh' as a shell alias/function that does the port knocking first and then passes any parameters to the normal /usr/bin/ssh.

Or you could make your own 'ssh' shell script somewhere else on the filesystem that handles it for you.

Re:Ask HN: Why is there no native SSH hook to run a local co... by dylan604 on Tuesday September 09, 2025 @07:36PM (#45191025)
without making it an alias, would this shell script somewhere else be usable by the other applications from the question? if you can provide the specific path rather than the system looking for whatever ssh was installed, then maybe, but i don't know those specifics which makes using the shell script have question marks.
Re:Ask HN: Why is there no native SSH hook to run a local co... by waqasx on Tuesday September 09, 2025 @07:46PM (#45191119)
You can write a script and put it in PATH
Re:Ask HN: Why is there no native SSH hook to run a local co... by ranger_danger on Tuesday September 09, 2025 @07:50PM (#45191170)
You could either put the script somewhere earlier within the PATH than wherever the normal ssh binary is, or manually export a new PATH with some other custom folder that comes first, which you could use to override anything, e.g. export PATH=/root/bin:$PATH
Re:Ask HN: Why is there no native SSH hook to run a local co... by capitainenemo on Tuesday September 09, 2025 @07:31PM (#45190983)
Can't you use Match exec?
Re:Ask HN: Why is there no native SSH hook to run a local co... by tetris11 on Wednesday September 10, 2025 @03:45AM (#45194528)
This is a fantastic answer. I'd never heard of the Match exec commands in SSH config. I'll have to play around with this - thank you!
Re:Ask HN: Why is there no native SSH hook to run a local co... by ranger_danger on Wednesday September 10, 2025 @10:10AM (#45197977)
Yea I would consider this very poorly documented, although I'm sure the authors would be quick to point out how dumb I am for even suggesting that.

I bet most ssh users don't even know about the -w option. It even supports layer2 tunnels!

And somehow https://wrongthink.link/posts/all-you-need-is-ssh/ does not even mention this usecase.

Re:Ask HN: Why is there no native SSH hook to run a local co... by tetris11 on Wednesday September 10, 2025 @12:47PM (#45200396)
I have to admit I'm not certain what use case the -w option serves. It seems like a fancy port forwarding?
Re:Ask HN: Why is there no native SSH hook to run a local co... by dylan604 on Wednesday September 10, 2025 @04:08PM (#45202907)
It's funny how after using an app like ssh/awk/sed/grep for so many years and achieving so much functionality with it with some pretty "advanced" features is still but a scratch on the surface of actual abilities or what other people do with them. I'd wear it like a badge of honor if I ever created something so useful and universally used. To the point, I'd probably even use my initials in the name of the app too!
Re:Ask HN: Why is there no native SSH hook to run a local co... by ranger_danger on Wednesday September 10, 2025 @03:29PM (#45202476)
Check the section in the manpage for ssh(1) named: SSH-BASED VIRTUAL PRIVATE NETWORKS
Re:Ask HN: Why is there no native SSH hook to run a local co... by capitainenemo on Monday September 15, 2025 @07:48PM (#45256338)
noticed these replies in my history... personally I think sshtunnel is a lot more convenient most of the time.
Re:Ask HN: Why is there no native SSH hook to run a local co... by bityard on Tuesday September 09, 2025 @07:39PM (#45191051)
OpenSSH was written by and is still maintained by those who adhere to The Unix Way. If you asked for this feature, they would probably tell you to just use a shell alias or function instead.