Thursday, August 5, 2010

read ip address of linux interfaces

you can read ip configuration with "ifconfig" command. but this command returns lots of information. if you need just some specific interface address, you have to use longer commands ;)

for example;

my notebook's current ifconfig command output is;

ismail@ismail-laptop:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:26:b9:9a:65:06
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Memory:f6ae0000-f6b00000

eth2 Link encap:Ethernet HWaddr c4:17:fe:1e:fe:6d
inet6 addr: fe80::c617:feff:fe1e:fe6d/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:35
TX packets:0 errors:12 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:17 Base address:0xc000

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:3582 errors:0 dropped:0 overruns:0 frame:0
TX packets:3582 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:456842 (456.8 KB) TX bytes:456842 (456.8 KB)

ppp0 Link encap:Point-to-Point Protocol
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:5463 errors:0 dropped:0 overruns:0 frame:0
TX packets:5612 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:5031133 (5.0 MB) TX bytes:1609578 (1.6 MB)


i'm using mobile modem now. so i am using ppp0 interface to connect to internet.

for example a)
i want to read only my ppp0 interface's ip address.

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{ print $2}'| awk -F ':' '{print $2}'
178.244.166.249


lets explain step by step this command;

ifconfig ppp0 command output is;

ismail@ismail-laptop:~$ ifconfig ppp0
ppp0 Link encap:Point-to-Point Protocol
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:5717 errors:0 dropped:0 overruns:0 frame:0
TX packets:5871 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:5204480 (5.2 MB) TX bytes:1678921 (1.6 MB)


i can eliminate other interfaces with ifconfig ppp0 command. if this output is sent to grep command with "|" argument, i can select only the line which has ip address included in. like this;

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr"
inet addr:178.244.166.249 P-t-P:10.64.64.64 Mask:255.255.255.255


i use "inet addr" keyword because, i think this keyword is unique for ifconfig command output. and now i need to start doing some string manipulation. for example we can seperate this line by blanks. awk command is able to do this features as following;

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{print $2}'
addr:178.244.166.249


-F means "find".
after than ' ' means: seperate by the blanks
and after than '{ print $2 }', prints the second seperated element. if we use $1, we can see "inet" output.

and last step. we have to use awk again for seperating by ":" keyword for read only ip address part.

ismail@ismail-laptop:~$ ifconfig ppp0 | grep "inet addr" | awk -F ' ' '{print $2}' | awk -F ':' '{ print $2 }'
178.244.166.249


using like things on last command and finally we can read only ip address.

and last trick. if you will use these commands on shell script and define crontab schedule you have to use fullpath for all commands. so, we have to find fullpath informations about using commands. we can use whereis command to find fullpath info.

ismail@ismail-laptop:~$ whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
ismail@ismail-laptop:~$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz
ismail@ismail-laptop:~$ whereis awk
awk: /usr/bin/awk /usr/lib/awk /usr/share/awk /usr/share/man/man1/awk.1.gz
ismail@ismail-laptop:~$


and final command;

ismail@ismail-laptop:~$ /sbin/ifconfig ppp0 | /bin/grep "inet addr" | /usr/bin/awk -F ' ' '{ print $2}' | /usr/bin/awk -F ':' '{ print $2 }'
178.244.166.249


In these examples, you have to know interface name to extract ip address informations. for example we knew ppp0 interface name. if you don't know interface name, you can read next entry :)

No comments:

Post a Comment

Thanks