{"id":666,"date":"2012-11-26T10:05:44","date_gmt":"2012-11-26T10:05:44","guid":{"rendered":"http:\/\/www.plugged.in\/?p=666"},"modified":"2019-04-17T19:59:18","modified_gmt":"2019-04-17T16:59:18","slug":"getting-network-information-in-bash-scripts","status":"publish","type":"post","link":"https:\/\/www.veriteknik.net.tr\/en\/getting-network-information-in-bash-scripts\/","title":{"rendered":"Getting Network Information in Bash Scripts"},"content":{"rendered":"<p>Sometimes when writing your bash scripts, you may need some information about the network, such as the IP addresses, both IPv4 and IPv6, broadcast addresses, netmasks and such. There are two very basic ways of getting the necessary information in Linux systems, you should either choose the <strong>ip addr show<\/strong> method, and parse what&#8217;s coming out of it, or parse the output of <strong>ifconfig<\/strong>. Let&#8217;s deal with them both.<\/p>\n<p><strong>The &#8220;ip addr show&#8221; method<\/strong><\/p>\n<p>We will parse this output:<\/p>\n<pre class=\"brush: text; gutter: true; first-line: 1\">eaydin@eaydin:~$ ip addr show\n1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 16436 qdisc noqueue state UNKNOWN \n\u00a0\u00a0\u00a0 link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n\u00a0\u00a0\u00a0 inet 127.0.0.1\/8 scope host lo\n\u00a0\u00a0\u00a0 inet6 ::1\/128 scope host \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 valid_lft forever preferred_lft forever\n2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000\n\u00a0\u00a0\u00a0 link\/ether 00:22:15:f6:55:e6 brd ff:ff:ff:ff:ff:ff\n\u00a0\u00a0\u00a0 inet 192.168.16.30\/24 brd 192.168.16.255 scope global eth0\n\u00a0\u00a0\u00a0 inet6 fe80::222:15ff:fef6:55e6\/64 scope link \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 valid_lft forever preferred_lft forever<\/pre>\n<p>IP ADDRESS :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $2}'| cut -d \"\/\" -f 1<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 192.168.16.30<\/pre>\n<p>PREFIX (Netmask in CIDR Notation) :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $2}'| cut -d \"\/\" -f 2<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 24<\/pre>\n<p>Broadcast IP :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $4}'<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 192.168.16.255<\/pre>\n<p>Device name :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show | awk 'FNR==7 {print $2}' | tr -d :<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : eth0<\/pre>\n<p>IPv6 ADDRESS :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show |grep -w inet6 |grep -v ::1|awk '{ print $2}'| cut -d \"\/\" -f 1<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : fe80::222:15ff:fef6:55e6<\/pre>\n<p>IPv6 PREFIX (Netmask in CIDR Notation) :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show |grep -w inet6 |grep -v ::1|awk 'FNR==1 { print $2}'| cut -d \"\/\" -f 2<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 64<\/pre>\n<p>Ethernet Card MAC Address :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ip addr show | grep -w ether | awk '{ print $2 }'<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 00:22:15:f6:55:e6<\/pre>\n<p><strong>The &#8220;ifconfig&#8221; method<\/strong><\/p>\n<p>This time we&#8217;ll parse this output:<\/p>\n<pre class=\"brush: text; gutter: true; first-line: 1\">eaydin@eaydin:~$ ifconfig\neth0      Link encap:Ethernet  HWaddr 00:22:15:f6:55:e6  \n          inet addr:192.168.16.30  Bcast:192.168.16.255  Mask:255.255.255.0\n          inet6 addr: fe80::222:15ff:fef6:55e6\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:626934 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:363506 errors:0 dropped:0 overruns:0 carrier:2\n          collisions:0 txqueuelen:1000 \n          RX bytes:284072710 (284.0 MB)  TX bytes:56413838 (56.4 MB)\n          Interrupt:45 \n\nlo        Link encap:Local Loopback  \n          inet addr:127.0.0.1  Mask:255.0.0.0\n          inet6 addr: ::1\/128 Scope:Host\n          UP LOOPBACK RUNNING  MTU:16436  Metric:1\n          RX packets:43339 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:43339 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0 \n          RX bytes:4834972 (4.8 MB)  TX bytes:4834972 (4.8 MB)<\/pre>\n<p>IP ADDRESS :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $2}' | cut -d \":\" -f 2<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 192.168.16.30<\/pre>\n<p>Broadcast IP :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $6}' | cut -d \":\" -f 2\n\nOutput : 192.168.16.255<\/pre>\n<p>Netmask :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $4}' | cut -d \":\" -f 2<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 255.255.255.0<\/pre>\n<p>Device name :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | awk 'FNR==1 { print $1 }' | tr --d :<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : eth0<\/pre>\n<p>IPv6 ADDRESS :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w inet6 | grep -v ::1| awk '{ print $2 }'\n\nOutput : fe80::222:15ff:fef6:55e6<\/pre>\n<p>IPv6 PREFIX (Netmask in CIDR Notation) :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w inet6 | awk 'FNR == 2 { print $4 }'<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 64<\/pre>\n<p>Ethernet Card MAC Address :<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">ifconfig | grep -w ether | awk '{ print $2 }'<\/pre>\n<pre class=\"brush: text; gutter: true; first-line: 1\">Output : 00:22:15:f6:55:e6<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes when writing your bash scripts, you may need some information about the network, such as the IP addresses, both IPv4 and IPv6, broadcast addresses, netmasks and such. There are two very basic ways of getting the necessary information in Linux systems, you should either choose the ip addr show method, and parse what&#8217;s coming [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[370,386],"tags":[544],"yst_prominent_words":[1046,1038,1048,1041,1043,1040,1035,1045,1037,1034,839,1039,1031,1036,1033,1044,1042,1032,1030,1047],"class_list":["post-666","post","type-post","status-publish","format-standard","hentry","category-linux","category-network","tag-linux-bash-network-ifconfig-ip"],"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Mustafa Emre Ayd\u0131n","author_link":"https:\/\/www.veriteknik.net.tr\/en\/author\/eaydin\/"},"uagb_comment_info":0,"uagb_excerpt":"Sometimes when writing your bash scripts, you may need some information about the network, such as the IP addresses, both IPv4 and IPv6, broadcast addresses, netmasks and such. There are two very basic ways of getting the necessary information in Linux systems, you should either choose the ip addr show method, and parse what&#8217;s coming&hellip;","_links":{"self":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":1,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":6539,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/666\/revisions\/6539"}],"wp:attachment":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/categories?post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/tags?post=666"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/yst_prominent_words?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}