Помещение IP-адрес в переменную bash. Есть ли лучший способ

Я пытаюсь найти короткий и надежный способ поместить мой IP-адрес в переменную bash, и мне было любопытно, есть ли более простой способ сделать это. Вот как я сейчас это делаю:

ip=`ifconfig|xargs|awk '{print $7}'|sed -e 's/[a-z]*:/''/'`
44
задан Duncan X Simpson 3 December 2015 в 22:26
поделиться

1 ответ

На Mac OSX можно использовать ipconfig getifaddr [interface] для получения локального IP:

$ ipconfig getifaddr en0
192.168.1.30
$ man ipconfig 

DESCRIPTION
     ipconfig is a utility that communicates with the IPConfiguration agent to
     retrieve and set IP configuration parameters.  It should only be used in
     a test and debug context.  Using it for any other purpose is strongly
     discouraged.  Public API's in the SystemConfiguration framework are cur-
     rently the only supported way to access and control the state of IPCon-
     figuration.

     ...

     getifaddr interface-name
                 Prints to standard output the IP address for the first net-
                 work service associated with the given interface.  The output
                 will be empty if no service is currently configured or active
                 on the interface.
0
ответ дан 26 November 2019 в 21:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: