Получить IP-адрес от ifconfig

394
Federico Ponzi

Я обновлял свой файл оболочки и думал, что было бы неплохо иметь псевдоним, показывающий только мой ip. Я обычно соединяюсь с Wi-Fi, который находится на wlan0, и это последний "блок" устройств.

Итак, я что-то вроде этого:

wlan0 Link encap:Ethernet HWaddr 6c:71:d9:d3:28:9d  inet addr:192.168.1.16 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::6e71:d9ff:fed3:289d/64 Scope:Link 

И я хочу мой адрес Bcast.

С помощью этой команды я могу получить это:

alias ip="ifconfig | grep Bcast | tail -n1 | cut -d' ' -f14 | cut -d':' -f2" 

Но мне это не нравится. Есть ли более чистый способ сделать это?

0
Если вы уверены, что `wlan0` - это интерфейс, который вы используете, то сработает следующее:` ifconfig wlan0 | sed -n 's /^.* Bcast: \ (. * \). * $ / \ 1 / p «'. Это аккуратнее? AFH 8 лет назад 2
Да, намного лучше, чем использовать 4 команды. Но не могли бы вы объяснить это? Спасибо Federico Ponzi 8 лет назад 0
Мне нужно сделать это как ответ. AFH 8 лет назад 0

1 ответ на вопрос

1
AFH

If you are certain that wlan0 is the interface, then you can return the information for just this interface with:

ifconfig wlan0 

Now that you have only one line with Bcast: in it (eliminating tail) you can use sed to combine the grep and cut functions:

ifconfig wlan0 | sed -n 's/^.*Bcast:\(.*\) .*$/\1/p' 

In this case, sed -n eliminates the routine printing of lines from input, and the search string matches the whole line, while marking what lies between Bcast: and the next space: the substitution is this marked field only, and /p overrides the -n and forces printing. Note that to assign this string to an alias you will need to use double quotes and escape some of the characters with back-slash, or use single quotes, replacing those in the above command by '\'' (though you can omit the resultant final '').

Finally, if you want to make sure that wlan0 is the interface you want, and there are no others configured for internet access, then you can find out which interface is being used (you may need to install networkctl):

networkctl status | sed -n 's/^.*Gateway: .* on \(.*$\)/\1/p' 

This uses sed on the networkctl output similarly to before. Now you can combine them:

ifconfig $(networkctl status |\ sed -n 's/^.*Gateway: .* on \(.*$\)/\1/p')|sed -n 's/^.*Bcast:\(.*\) .*$/\1/p' 

This is hardly neat, but it covers most of the bases - in particular, if your wireless isn't working and you use Ethernet instead, it should still work. If you have both interfaces working together, it becomes more complicated still: I'll leave you to work it out (hint: you'll need to use /Gateway/,$ as the line range for the s command, and a way to exclude the IPv6 entries).

Вывод `ip -4 -o dev wlan0` может быть легче проанализировать Teun Vink 8 лет назад 0
@TeunVink - Моя версия `ip` (Ubuntu 15.04) не имеет` dev` в качестве объекта. Информация находится в `ip -4 -o addr`, но я не думаю, что это легче разобрать. AFH 8 лет назад 0

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