0

I have a couple of hosts configured in ~/.ssh/config, for example:

Host SomeHost
  Hostname 10.0.0.3
  User SomeUser

I'm trying to get the hostname part (10.0.0.3) as a variable from inside a shell script (the %h variable). Is it somehow possible?

3
  • 1
    Short of parsing the file yourself, no. The configuration doesn't exist outside of the file until ssh reads it when executed, and ssh doesn't provide an API for querying configurations. Commented Aug 31, 2014 at 14:53
  • 2
    This sounds like an XY problem -- what problem are you're really trying to solve? Commented Aug 31, 2014 at 15:32
  • @glennjackman I'm trying to use nmap to see if the remote host has port 22 open. It is much faster than using ssh to use a test connection. Commented Sep 1, 2014 at 8:29

1 Answer 1

1
host2ip() {
    awk -v host="$1" '
        $1 == "Host" && $2 == host {have_host = 1}
        have_host && $1 == "Hostname" {print $2; exit}
    ' ~/.ssh/config
}

ip=$(host2ip SomeHost)
echo $ip
10.0.0.3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.