1

I need an ip address of a local host in order to use it in an url string. I want to connect to other computer by using port forwarding. The connecting will be written in a bash script.

So, is there a way to generically parse an ip address of a localhost?

MarsaPalas
  • 375
  • 1
  • 6
  • 19
  • Any suggestions? Perhaps I'm not clear enough? – MarsaPalas Apr 24 '14 at 14:25
  • 1
    possible duplicate of [How to I get the primary IP address of the local machine on Linux and OS X?](http://stackoverflow.com/questions/13322485/how-to-i-get-the-primary-ip-address-of-the-local-machine-on-linux-and-os-x) – Anders Lindahl Apr 24 '14 at 14:26
  • Why from **localhost** only you want use any command/way to find IPaddress of machine out of so many available!!!! – PradyJord Apr 24 '14 at 14:26
  • 1
    See also: http://unix.stackexchange.com/questions/42173/ip-of-localhost – Josh Jolly Apr 24 '14 at 14:27
  • Thank you, so it's this: ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' – MarsaPalas Apr 24 '14 at 14:39

2 Answers2

7

Translated into an IP address, commonly a localhost is always designated as 127.0.0.1. So generally, you can consider the loop back address same for every machine.

To get address other than 127.0.0.1 you may use the following bash command: hostname -i.

Qasim
  • 5,181
  • 4
  • 30
  • 51
1

One of many solutions:

localhost_ip_address=`ping -c 1 localhost | head -n 1 | cut -d'(' -f2 | cut -d')' -f1`
aisbaa
  • 9,867
  • 6
  • 33
  • 48
  • or you can run in terminal only this: `ping -c 1 localhost | head -n 1 | cut -d'(' -f2 | cut -d')' -f1` – Eldar Jul 06 '21 at 23:56