1

I would like to get the ip address of a .local hostname, so I can ssh into it, seems like host hostname.local does not work in this scenario

ssh [email protected] does not work, so I am trying to get ssh [email protected] instead.

The command I found which will convert a .local hostname (not dns) to an ip address is the humble ping command.

Heres the results of ping

PING 5153F344.local (192.168.8.105) 56(84) bytes of data.
64 bytes from 192.168.8.105: icmp_seq=1 ttl=64 time=0.524 ms

--- 5153F344.local ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.524/0.524/0.524/0.000 ms

This is one of the many variants of my failing solution:

# Note trying not to use single quotes so I can turn all these commands into an alias:
DEV_TERM_IP="$(ping -c 1 5153F344.local) | cut -d \"(\" -f2 | cut -d \")\" -f1 | echo"
ssh "me@$DEV_TERM_IP.local"
run_the_race
  • 283
  • 3
  • 12
  • 1
    Apart from your command substitution being wonky (the ending `)` should be after `-f1` and I don't know why you have `echo` in there, and the `cut` call can only use single character delimiter, so drop the literal quotes, and you get multiple lines from `ping`, so you'll get multiple lines from `cut` too), the following may also be relevant: https://unix.stackexchange.com/questions/341093/i-can-resolve-a-local-domain-ping-the-ip-but-i-cant-ping-this-domain – Kusalananda Jan 27 '22 at 09:30
  • 1
    It is curious that `ssh` doesn’t resolve IP addresses like `ping` does. Both use `libresolv` on my Debian system. Have you got something like `ssh: Could not resolve hostname test.local: No address associated with hostname` ? – Frédéric Loyer Jan 27 '22 at 09:31
  • Ack @FrédéricLoyer, I looked intro it more closely, one of the port numbers was mirrored. Its working without the ip address now – run_the_race Jan 27 '22 at 09:33
  • Thank @they that help me understand how to craft a better command. – run_the_race Jan 27 '22 at 09:33

1 Answers1

3

I would try

 DEV_TERM_IP=$(ping -c 2 localhost| awk -F '[()]' '/PING/ { print $2}')

where

  • -F '[()]' tell awk to use either ( or ) as separator
  • /PING/ grep line with PING
  • { print $2} print second field
Archemar
  • 31,183
  • 18
  • 69
  • 104