{"id":5379,"date":"2015-10-26T12:57:12","date_gmt":"2015-10-26T04:57:12","guid":{"rendered":"http:\/\/rmohan.com\/?p=5379"},"modified":"2015-10-26T12:57:25","modified_gmt":"2015-10-26T04:57:25","slug":"ip-address-with-ifconfig-and-sed","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5379","title":{"rendered":"IP address with ifconfig and sed"},"content":{"rendered":"<p>Well, my new favorite approach is to pipe <code>ifconfig<\/code> into a single <code>sed<\/code> command \ud83d\ude42<\/p>\n<div>ifconfig | sed -n -e &#8216;s\/:127\\.0\\.0\\.1 \/\/g&#8217; -e &#8216;s\/ *inet addr:\\([0-9.]\\+\\).*\/\\1\/gp&#8217;<\/div>\n<p>&nbsp;<\/p>\n<p>So, how does it work? Well, there are two filters (aka scripts, ie the parameters after the <code>-e<\/code> flags). The first one, <code>s\/:127\\.0\\.0\\.1 \/\/g'<\/code>, simply strips out all occurrences of the local loopback address (127.0.0.1) &#8211; this can be left out if you want to include the loopback address in the results. And the second filter, <code>'s\/ *inet addr:\\([0-9.]\\+\\).*\/\\1\/gp'<\/code> matches all lines with IP addresses, strips all but the IP address itself, and prints the matching line (note the &#8216;<code>p<\/code>&#8216; at the end of the filter, which works in with the <code>-n<\/code>flag at the stared of the <code>sed<\/code> command).<\/p>\n<p>Obviously that explanation is only meant to be a brief summary. You should consult the <code>sed<\/code> manual is you&#8217;re still unsure of how \/ why this works&#8230; or post a question below \ud83d\ude42<\/p>\n<p>Just for comparison, here&#8217;s some examples of how others do the same thing:<\/p>\n<ul>\n<ul>\n<li><a title=\"Read UNIX\/Linux system IP address in a shell script\" href=\"http:\/\/www.cyberciti.biz\/tips\/read-unixlinux-system-ip-address-in-a-shell-script.html\" target=\"_blank\">Read UNIX\/Linux system IP address in a shell script<\/a>\n<div>ifconfig | grep &#8216;inet addr:&#8217;| grep -v &#8216;127.0.0.1&#8217; |<br \/>\ncut -d: -f2 | awk &#8216;{ print $1}&#8217;<\/div>\n<p>Notice that this one uses an incorrect loopback filter (<code>'127.0.0.1'<\/code> should be <code>':127\\.0\\.0\\.1 '<\/code>) and yet my new favorite is still shorter \ud83d\ude09<\/li>\n<li><a title=\"Parsing IP Address from ifconfig\" href=\"http:\/\/www.diatoid.info\/?p=3\" target=\"_blank\">Parsing IP Address from ifconfig<\/a>\n<div>ifconfig eth0 | grep \u201cinet\u201d | awk \u2018{print $2}\u2019 | awk -F: \u2018{print $2}\u2019<\/div>\n<p>Pretty reasonable, but two more pipes than mine \ud83d\ude42<\/li>\n<li><a title=\"Setting up a 6to4 IPv6 tunnel with Debian\" href=\"http:\/\/www.drbeat.li\/bbstatic\/software\/debian\/\" target=\"blank\">Setting up a 6to4 IPv6 tunnel with Debian<\/a>\n<div>ifconfig $EXTIF | grep &#8216;inet addr&#8217; | cut -d : -f2 | cut -d &#8216; &#8216; -f1<\/div>\n<p>This is almost identical to how I used to do it &#8211; before I became familiar with <code>sed<code> ;)<\/code><\/code><\/li>\n<\/ul>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><code><code><code><a title=\"Advance Linux commands\" href=\"http:\/\/www.host.co.in\/forums\/f8\/advance-linux-commands-931\/\" target=\"blank\">Advance (sic) Linux commands<\/a><br \/>\n<\/code><\/code><\/code><\/p>\n<div>ifconfig | grep -vw inet6 | grep -w inet | cut -d : -f 2 | cut -d \\ -f 1<\/div>\n<p>Ok, but a little bit redundant.<\/li>\n<li><code><code><code><a title=\"ifconfig eth0 and awk and grep\" href=\"http:\/\/www.unix.com\/unix-dummies-questions-answers\/27906-ifconfig-eth0-awk-grep.html\" target=\"_blank\">ifconfig eth0 and awk and grep<\/a><br \/>\n<\/code><\/code><\/code><\/p>\n<div>ifconfig eth0 | head -n 2 \\<br \/>\n| sed &#8216;N;s\/\\n\/ \/;N;s\/\\n\/ \/&#8217; | awk &#8216;{print $7 &#8221; &#8221; $9 &#8221; &#8221; $5}&#8217; \\<br \/>\n| sed -e &#8216;s\/addr:\/\/g&#8217; -e &#8216;s\/Mask:\/\/g&#8217;<\/div>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, my new favorite approach is to pipe ifconfig into a single sed command \ud83d\ude42<\/p>\n<p> ifconfig | sed -n -e &#8216;s\/:127\\.0\\.0\\.1 \/\/g&#8217; -e &#8216;s\/ *inet addr:\\([0-9.]\\+\\).*\/\\1\/gp&#8217; <\/p>\n<p>&nbsp;<\/p>\n<p>So, how does it work? Well, there are two filters (aka scripts, ie the parameters after the -e flags). The first one, s\/:127\\.0\\.0\\.1 \/\/g&#8217;, simply strips out all [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5379"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5379"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5379\/revisions"}],"predecessor-version":[{"id":5380,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5379\/revisions\/5380"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}