Как узнать ip адрес интерфейса и вывести только его

Маленький пример о том как использовать ifconfig, cut и awk чтобы вывести только ip адрес. Вывод команды ifconfig будет таким:

$ /sbin/ifconfig

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:69527 errors:0 dropped:0 overruns:0 frame:0
          TX packets:69527 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:41559546 (39.6 MiB)  TX bytes:41559546 (39.6 MiB)
eth0      Link encap:Ethernet  HWaddr 00:17:9A:0A:F6:44
          inet addr:192.168.2.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::217:9aff:fe0a:f644/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:227614 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60421 errors:0 dropped:0 overruns:0 carrier:0
          collisions:272 txqueuelen:1000
          RX bytes:69661583 (66.4 MiB)  TX bytes:10361043 (9.8 MiB)
          Interrupt:17
ra0       Link encap:Ethernet  HWaddr 00:50:56:C0:00:01
          inet addr:192.168.1.2  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1024 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1320 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

Выведем нужный нам интерфейс и исключим все, кроме строки с IP-адресом:

$ /sbin/ifconfig eth0| grep 'inet addr:'
inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0

Теперь подключим утилиту cut для того чтобы отделить IP. Говоря простым языком, cut разделит строку в виртуальный массив через “:” и возьмет 2-й элемент этого массива. Вывод будет таким:

$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2
192.168.2.1  Bcast

Ну и в конце с помощью awk вытаскиваем IP. Финальная команда будет выглядеть так:

$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
192.168.2.1

Еще вариант:

$ /sbin/ifconfig eth0 | awk '/inet addr:/ {print substr($2, 6)}'

И еще:

ifconfig eth0 | sed -n '/inet addr:/s/ *inet addr:\([[:digit:].]*\) .*/\1/p'

Выбирайте любой понравившийся!

42 комментария к “Как узнать ip адрес интерфейса и вывести только его

  1. Oh my benefits! an outstanding article guy. Thank you However I am experiencing concern with ur rss. Don?t understand why Incapable to subscribe to it. Is there any individual getting identical rss issue? Anyone that recognizes kindly react. Thnkx

  2. Good article. I discover something more tough on different blog sites day-to-day. It will always be boosting to read material from various other writers and also practice a something from their store. I?d prefer to use some with the material on my blog site whether you don?t mind. Natually I?ll give you a web link on your internet blog site. Many thanks for sharing.

  3. Can I just state what an alleviation to find somebody who in fact understands what theyre talking about online. You most definitely recognize exactly how to bring an issue to light and make it important. Even more individuals need to read this and also comprehend this side of the story. I cant believe youre not much more preferred due to the fact that you certainly have the gift.

  4. Excellent post. I used to be checking continuously this blog
    and I am impressed! Very useful info specifically the last phase :
    ) I care for such information much. I was seeking this
    particular info for a long time. Thanks and good
    luck.

  5. Hello There. I found your blog using msn. This is a really well written article.
    I will make sure to bookmark it and return to read more of your
    useful info. Thanks for the post. I’ll certainly comeback.

  6. After research a few of the post on your web site currently, and also I genuinely like your means of blogging. I bookmarked it to my bookmark website checklist and will be inspecting back quickly. Pls have a look at my internet site as well and also let me recognize what you believe.

  7. Excellent pieces. Keep writing such kind of info on your site.

    Im really impressed by your blog.
    Hello there, You have performed an excellent job.
    I’ll definitely digg it and in my view suggest to
    my friends. I am confident they will be benefited
    from this web site.

  8. There are some interesting times in this write-up yet I don?t understand if I see every one of them facility to heart. There is some legitimacy however I will certainly hold opinion till I look into it further. Good article, many thanks and we desire more! Added to FeedBurner also

  9. Wow that was unusual. I just wrote an really long comment but after
    I clicked submit my comment didn’t appear. Grrrr… well I’m
    not writing all that over again. Anyhow, just wanted to say wonderful
    blog!

  10. Aw, this was a truly nice message. In concept I wish to place in composing similar to this additionally? taking some time and actual effort to make an excellent short article? yet what can I say? I hesitate alot as well as never seem to get something done.

  11. The next time I review a blog, I wish that it does not disappoint me as long as this one. I indicate, I know it was my selection to review, but I actually thought youd have something interesting to claim. All I listen to is a lot of grumbling regarding something that you might take care of if you werent as well busy seeking interest.

  12. Good blog post. I find out something more difficult on different blog sites everyday. It will certainly constantly be stimulating to read material from other authors and also practice a something from their shop. I?d like to use some with the content on my blog whether you don?t mind. Natually I?ll provide you a web link on your internet blog. Many thanks for sharing.

  13. Can I simply claim what an alleviation to discover a person that in fact understands what theyre discussing on the net. You definitely know just how to bring a concern to light and also make it vital. More individuals need to read this as well as understand this side of the tale. I cant think youre not more popular due to the fact that you absolutely have the gift.

  14. Wonderful website you have here but I was wanting to know if you knew of any discussion boards that cover the same topics discussed here? I’d really like to be a part of online community where I can get suggestions from other knowledgeable individuals that share the same interest. If you have any suggestions, please let me know. Thank you!

  15. Hey just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I’m not sure if this is a format issue or something to do with web browser compatibility but I figured I’d post to let you know. The design and style look great though! Hope you get the problem resolved soon. Many thanks

  16. Hi there are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any coding expertise to make your own blog? Any help would be really appreciated!

  17. Hello! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be great if you could point me in the direction of a good platform.

  18. Very good blog! Do you have any recommendations for aspiring writers? I’m planning to start my own blog soon but I’m a little lost on everything. Would you recommend starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused .. Any recommendations? Kudos!

  19. Does your site have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.

  20. I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Outstanding work!

  21. Does your blog have a contact page? I’m having a tough time locating it but, I’d like to send you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.

  22. Hi, extremely interesting article. My sister and I have been recently looking to find comprehensive info on this subject kind of stuff for a short time, but we didn’t until now. Do you think you can also make some youtube videos about this, I believe your website will be far more detailed in case you did. In any other case, oh well. I will be checking out on this website in the forseeable future. Email me to keep me up to date. granite countertops cleveland

  23. Nice post. I find out something more challenging on diverse blogs everyday. It will always be stimulating to read content using their company writers and rehearse a little at their store. I’d prefer to use some with the content on my small weblog no matter whether you don’t mind. Natually I’ll provide a link on your web weblog. Thanks for sharing.

  24. When I originally commented I clicked the -Notify me when new comments are added- checkbox and currently each time a comment is added I obtain four emails with the same comment. Exists any way you can eliminate me from that solution? Many thanks!

Оставить ответ

Ваш адрес email не будет опубликован. Обязательные поля помечены *