Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

Host pro-10
Hostname 192.168.1.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

3 Answers 3

up vote 8 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:

192.168.1.10 pro10
192.168.1.11 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

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}
do  
  echo -e "Host pro-$i\nHostname 192.168.1.$i\nProxyCommand ssh production-server nc %h %p\n"; > ~/.ssh/config
done

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

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);
do
    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
done

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

Your Answer

 
discard

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.