Devon Ville
Devon Ville

Reputation: 2091

expand ssh hostname in shell script

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?

Upvotes: 0

Views: 437

Answers (1)

glenn jackman
glenn jackman

Reputation: 247002

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

Upvotes: 1

Related Questions