0

my goal is to be able to type:

  • srv01.sms and it connects to srv01.anything0.example

  • srv02.mail and it connects to srv02.anything1.example

I know that i can edit my .ssh/config file to do something like this, but i don't want to edit my .ssh/config file for 500 servers

Is there a way to do this? In ssh config? bash aliases?

fabolous05
  • 59
  • 1
  • 9
  • Possible duplicate: [How do I create a command prefix for bash?](https://unix.stackexchange.com/q/697465/108618) You want `ssh` where the other user wants `sudo`, but the rest seems similar. – Kamil Maciorowski Apr 05 '22 at 08:13
  • I thought it was clear you want to type `srv01.sms`, but the answer that "really helped you" requires you to type `ssh srv01.sms`, so I'm confused. Your comment under the answer makes me believe that `srv01.smsandmore.example` is not really what you want, possibly `srv01.andmore.example` or something. Ideally the question like yours should state clear requirements and stick to them. You can [edit] (a good step after "sorry that I didn't wrote that"), but remember an edit that invalidates existing answer(s) is not a good thing. Please at least clarify if typing `ssh srv01.sms` is acceptable. – Kamil Maciorowski Apr 05 '22 at 15:47
  • Depending on what you are trying to do with your ssh connections, you may want to look at using ansible in order to leverage dynamic inventories – Jesusaur Apr 06 '22 at 18:58

1 Answers1

4

If you only have very few patterns (sms/mail), but a large number of servers, then you can use the patterns supported by ssh_config

A pattern consists of zero or more non-whitespace characters, ‘*’ (a wildcard that matches zero or more characters), or ‘?’ (a wildcard that matches exactly one character).

(from ssh_config(5)).

along with the %h (the remote hostname) token in the Hostname specification:

Host srv*.sms
    Hostname %handmore.example


Host srv*.mail
    Hostname %handmore.example

Now any attempt to run ssh srv<xyz>.sms will connect to srv<xyz>.smsandmore.example, likewise for .mail.

Wieland
  • 6,353
  • 3
  • 28
  • 31
  • Great answer this really helped me! Sorry that i didn't wrote that in my question but the %h takes the full line. Do you know how it only takes everything that is in front of the . ? – fabolous05 Apr 05 '22 at 09:44
  • I don't know if it's possible to only match the part before the . with SSH. – Wieland Apr 06 '22 at 11:37