≡ Menu

inet addr

IPv6 Address Example

What does an IPv6 address look like? Can you give an example of IPv6 address? How do I view an IPv6 address under Linux or UNIX operating systems?
[click to continue…]

Q. How do I find out my IP address assigned to eth0 or ra0 interface using perl?

A. If you need to know the IP address of the UNIX / Linux machine you are running on, use the following perl one liner. Perl don't have any inbuilt facility but combination of ifconfig command ans shell pipes you can craft something as follows to display your system IP address:

Find my IP address using Perl One liner and shell pipes

Type the following command at a shell prompt:

ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }'

Q. I need to get the IP address assigned to eth0 Linux interface. How do I find out IP address only? I don't want other information displayed by Linux ifconfig command.

A. For shell script or may be for other cause you may need the IP address only. You can use ifconfig command with grep and other filters.

Default output of /sbin/ifconfig command is all interfaces:
$ /sbin/ifconfigOutput:

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)

Now you just select eth0 as follows:
$ /sbin/ifconfig eth0

Now you just wanted the IP address, use grep to get the IP:
$ /sbin/ifconfig eth0| grep 'inet addr:'Output:

inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0

To get IP address from use cut command:
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2Output:

192.168.2.1  Bcast

Finally remove Bcast with awk
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
Output:

192.168.2.1

See how to read UNIX/Linux system IP address in a shell script

HowTo: Find Out Router Mac Address

How do I display or find out the MAC address for my small home / business router?
[click to continue…]

Bash Shell Command to Find or Get IP address

How do I find out my Linux / UNIX system ip address, subnet and related networking information from a command prompt?
[click to continue…]