Pregunta

I discovered only last week with help on here that I could have the OS Architecture (32/64) stored in a variable like this: arch=$(getconf LONG_BIT)

I am hoping that I can use a similar method for local IP address: ipaddress=$(????)

So that any occurrences of $ipaddress will be replaced with "192.168.1.100" or whatever the local IP might be

¿Fue útil?

Solución

Unfortunately, it is not possible to extract the IP address of the system from the sysconf sub-system.

It is possible to have several IP addresses associated with the interfaces on a Linux system so there is no way to determine which IP address is the 'correct' one.

You could use the following shell snippet to list the active IP addresses on the system:

ifconfig  | grep 'inet addr' | awk '{print $2}' | cut -d ':' -f2

On my system this lists the following IP addresses:

192.168.1.12
127.0.0.1

If you see similar output, it means you've only got a single active interface along with the local loopback, so you can add |grep -v 127 to the previous snippet to filter out the local loopback address.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top