Score:0

How to edit the middle of host name (%h) in ssh config?

br flag

In ssh config you can easily edit a hostname by appending or prepending it with something. For example, when you want to connect to server with ssh test5, but actually the server name is test5.mail, you can specify the server as like this:

Match exec "echo %h | grep -q '^test[0-9]$'"
    HostName %h.mail

The same approach could be applied if you need to prepend hostname.

But what if I want to edit the middle of the hostname? For example, I want to connect as ssh server1.storage, but the actual address I want to connect to is server1.int.storage.

In other words, I want to insert substring in the middle of %h.

Score:0
ke flag

you can just the nc bit in proxycommand without repeating ssh

Match exec "echo %h | grep -q '^server[0-9]*.storage$'"
    ProxyCommand nc $(echo %h | sed 's/storage/int.storage/') %p

in my case i used it for non dns resolvable aws ec2 names

Match exec "echo %n | grep -qE '^ip-.*\.compute\.internal$'"
ProxyCommand nc $(echo %n | cut -d. -f1 | cut -d- -f2- | tr - .) %p
Score:0
br flag

The following worked for me:

Match exec "echo %h | grep -q '^server[0-9]*.storage$'"
    ProxyCommand ssh $(echo %h | sed 's/storage/int.storage/') nc $(echo %h | sed 's/storage/int.storage/') %p

I have found example of proxy command in this question. But I do not completely understand why you need that part with nc and %p. There you specify the edit command twice. I would like to just specify it once, like this:

ProxyCommand ssh $(echo %h | sed 's/storage/int.storage/')

but it is not working.

From the same question linked above, I see that you can also specify the handler script. But it seems the command again should be specified with twice ediding. So I would rather prefer to keep everything in one place - in the ssh config itself.

Would appreciate explanation and other variants of solutions, if any.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.