Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two set of server. Test on production. All server have internal ip. currently I am using alias to login:

Host pro-10
ProxyCommand ssh production-server nc %h %p

For every server I need to repeat this rule. I am looking for a regualr expression way for doing this ?

Host pro-*
Hostname 192.168.1.$1
ProxyCommand ssh production-server nc %h %p

Is there a way of doing this ?

share|improve this question
up vote 10 down vote accepted

No, ssh doesn't support regular expression in ssh_config (1) and the example you gave aren't regular expressions. ssh_config (1) supports PATTERNS, i.e. you can define a pattern for your IPs, i.e:

Host 192.168.1.*
  ProxyCommand ssh production-server nc %h %p

and you should be able to have forwarding for all your internal IPs. Another solution would be to add entries to /etc/hosts for the specific IPs, i.e: pro10 pro11

and create one entry in your ~/.ssh/config file:

Host pro1?
  ProxyCommand ssh production-server nc %h %p
share|improve this answer
thanks for answer. Pattern will not work for me as it is internal ip, so Ip b/s test and production system will be getting conflicted. – Vivek Goel Jan 6 '13 at 6:47

I don't believe that a regular expression is possible to dynamically set up the SSH configuration. However, you should be able to use a for loop in Bash to automatically create the three lines in the config file for each server. For this example, I will assume you need to create entries for servers pro-75 to pro-125.

for i in $(seq 75 125);
    echo "Host pro-$i" >> ~/.ssh/config
    echo "Hostname 192.168.1.$i" >> ~/.ssh/config
    echo "ProxyCommand ssh production-server nc %h %p" >> ~/.ssh/config
    echo "\n" >> ~/.ssh/config

This will build a list of all numbers from 75 to 125 and store that as the list for the loop. It then loops through each member of the list and replaces $i. Each line is appended to the end of the SSH user config file. Finally, I included the newline to break up the configuration file just a little bit.

If you have specific values which are needed on the list, rather than a contiguous segment of numbers, then build a list manually, like this:

1 23 45 67 89
share|improve this answer

No it's not possible unfortunately. As you probably already know you can use the %h expansion, but that's about it.

In your case why not use a for loop?

for i in {4..10}
  echo -e "Host pro-$i\nHostname 192.168.1.$i\nProxyCommand ssh production-server nc %h %p\n"; >> ~/.ssh/config

The only other option I could come up with is, maybe create DNS entries with regexp, if your server supports this and then use those.

share|improve this answer

with ProxyCommand its possible - because its just a set of shell commands:

Host pro-*
ProxyCommand ssh production-server nc 192.168.1.`echo "%h" | perl -pe 's/pro-//'` %p

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.