Linux proc ip address

How to get the IPV4 address for an interface from /proc

Is there a clean, simple way to get an IP address for a network interface from /proc , similar to the way I can get the MAC address for a network interface?
Ideally I would just type cat /proc// and get the IPv4 address. I’d rather not run anything other than cat .

3 Answers 3

Under the /proc directory, you can also find the IPv4 addresses in the Forwarding Information Base table, at /proc/net/fib_trie

The table is pretty intelligible doing a mere cat , first comes the Main: and then Local:

or to see your network, IP addresses and netmask:

cat /proc/net/fib_trie | grep "|--" | egrep -v "0.0.0.0| 127." |-- 193.136.1.0 |-- 193.136.1.2 |-- 193.136.1.255 |-- 193.136.1.0 |-- 193.136.1.2 |-- 193.136.1.255 

They appear in a pre-determined order; probably priority, but that can be changed; other than that, no.

there is no ip command many of official docker containers, then your answer solved my problem. awk ‘/32 host/ < print f >‘ /proc/net/fib_trie | sort | uniq | grep -v 127.0.0.1

@God could you pl explain the awk syntax? The /32 host/ would match lines containing that 32 host . For me, these are lines like 32 host LOCAL . $2 would return the 2nd field from the line. so, what does print f do? . and what would be its value? Why does f=$2 come after print 2 . (I thought I knew awk enough.)

@Harry It basically does this: For every line it saves $2 in f. But if the line contains «32 host» then print f that was saved from the previous line. After printing f, save $2 again

It might be possible to associate network addresses from /proc/net/fib_trie with interfaces from /proc/net/route :

 $ awk '/^[^I]/ ' /proc/net/route br0 0001020A br0 FE01020A 

..and to convert the hexadecimal network addresses with:

$ echo FE01020A | xxd -r -p | hexdump -e '/1 "%u."' | tac -s'.' | sed 's/\.$//' 10.2.1.254 

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Читайте также:  Linux and ntp server

Источник

Get local network interface addresses using only proc?

@forest: Because it implies parsable text, dependence on modern Linux, and doesn’t require spawning processes.

I see. Sadly, I don’t know that the /proc data structures you’d need are guaranteed to remain consistent between operating systems or even kernel releases. (Someone please prove me wrong.) Meanwhile, the ifconfig and ip programs produce stable output, which is why I ended up choosing to parse it instead of turning to /proc. Here’s an alternative that looks promising: pypi.python.org/pypi/dnet

11 Answers 11

/proc/net/fib_trie holds the network topography

To simply print the addresses of all adapters:

To determine the adapter of those addresses (a) consult the adapters’ destination networks from /proc/net/route , (b) match those networks with the ones of /proc/net/fib_trie and (c) print the corresponding /32 host addresses listed under those networks.

Again no python unfortunately, but a quite awky bash approach:

#!/bin/bash ft_local=$(awk '$1=="Local:" flag' ' ' ' <<< $net_hex) awk '/'$net_dec'//32 host/ flag END '  
eth0: 192.168.0.5 255.255.255.0 lo: 127.0.0.1 255.0.0.0 wlan0: 192.168.1.14 255.255.255.0 

This approach does not work reliably for host addresses that share the network with other host addresses. This loss of network uniqueness makes it impossible to determine the correct host address from fib_trie as the order of those addresses does not necessarily match the order of networks of route.

Having said that, I'm not sure why you would want multiple host addresses belonging to the same network in first place. So in most use cases this approach should work just fine.

How do you match the ip address from fib_trie to the network interface name in /proc/net/route? Inspecting my /proc/net/route, I have multiple entries for the same network interface name. Furthermore, the ip address from fib_trie doesn't seem to match the 'destination' column, which is the IP converted from hex and reverse order, of ANY entries in the route table .

@Maytas Monsereenusorn Both fib_trie & route have in common that they provide the NETWORK IP address. Generally this network is unique to the HOST IP address. Thus you can link the corresponding interface from route with the corresponding host IP address from fib_trie. You can have multiple host IPs for the same interface, but they should not belong to the same network (see limitation above as to why). Is it possible that you are including host routes when speaking of 'multiple entries for the same network interface name' in /proc/net/route? Those entries are not considered by the script.

You may find the output of ip addr show easier to parse than output from other tools:

$ ip addr show 1: lo: mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:24:1d:ce:47:05 brd ff:ff:ff:ff:ff:ff inet 192.168.0.121/24 brd 192.168.0.255 scope global eth0 inet6 fe80::224:1dff:fece:4705/64 scope link valid_lft forever preferred_lft forever 3: eth1: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:24:1d:ce:35:d5 brd ff:ff:ff:ff:ff:ff 4: virbr0: mtu 1500 qdisc noqueue state UNKNOWN link/ether 92:e3:6c:08:1f:af brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 inet6 fe80::90e3:6cff:fe08:1faf/64 scope link valid_lft forever preferred_lft forever 

Another option is the file /proc/net/tcp . It shows all currently-open TCP sessions, which is different than what you asked for, but might be Good Enough.

$ cat tcp sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode 0: 00000000:0050 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 13536 1 ffff88019f0a1380 300 0 0 2 -1 1: 00000000:1355 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 19877854 1 ffff880016e69380 300 0 0 2 -1 2: 017AA8C0:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 13633 1 ffff88019f0a1a00 300 0 0 2 -1 3: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 8971 1 ffff88019f0a0000 300 0 0 2 -1 4: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 12952880 1 ffff880030e30680 300 0 0 2 -1 5: 00000000:0539 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 14332 1 ffff88019f0a2080 300 0 0 2 -1 6: 00000000:C000 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 14334 1 ffff88019f0a2700 300 0 0 2 -1 7: 0100007F:0A44 00000000:0000 0A 00000000:00000000 00:00000000 00000000 119 0 51794804 1 ffff880016e6a700 300 0 0 2 -1 8: 7900A8C0:B094 53D50E48:01BB 01 00000000:00000000 00:00000000 00000000 1000 0 64877487 1 ffff880100502080 23 4 16 4 -1 9: 7900A8C0:9576 537F7D4A:01BB 06 00000000:00000000 03:00000E5D 00000000 0 0 0 3 ffff880100c84600 10: 7900A8C0:CC84 0CC181AE:01BB 01 00000000:00000000 00:00000000 00000000 1000 0 61775908 1 ffff880198715480 35 4 11 4 -1 $ irb irb(main):001:0> [0x79, 0x00, 0xa8, 0xc0] => [121, 0, 168, 192] 

My IP is 192.168.0.121 ; note the funny arithmetic to make it come out right. 🙂

Источник

Read the current IP address from the filesystem? [closed]

I'm writing a program that needs to know the system's current IPv4 address (received via DHCP), if it's connected and has one. For the sake of argument, the language is irrelevant and I must read from a file on the disk. Is there such a file that always stores the current IPv4 address?

If you tell us what language it is, we could suggest to use that language API instead of trying to read it from a file.

I happen to be using Dart, though golang would also be of interest. I wanted to keep this question somewhat generic and language agnostic, though, if there was a method that only relied on the filesystem.

4 Answers 4

There is a file /proc/net/tcp which stores the IP address in the little-endian four-byte hexadecimal number format. However, this has the assumption that a session is open to find the IP address. Other than that, you could use ip addr show command as well.

You need to reverse the string to get the IP address. Refer to this answer on how to get the output from the file /proc/net/tcp .

Looks like it's actually a big-endian value interpreted as the machine endianness, which means it ends up reversed on little-endian machines. lxr.free-electrons.com/source/net/ipv4/tcp_ipv4.c

@Random832, thanks for the info. If you feel, the answer needs to be modified, you can go ahead and correct it 🙂

/proc/net/tcp will only show you TCP connections and listening TCP sockets. If you have an IP address which no socket is directly bound to and which is not currently used by a TCP connection, it will not show up in /proc/net/tcp .

There's no file on disk that's guaranteed to contain the current IP address. If you obtained your IP address through DHCP, the DHCP probably wrote the address somewhere, but there is no standard location.

The normal, portable way to obtain the current IP address would be to parse the output of ifconfig or (Linux-only) ip addr show . Note that in addition to the address of the primary Internet connection, there is also the loopback address 127.0.0.1, and often there are more (for internal networks, virtual machines, etc.). A good hint is to retrieve the address of the interface that provides the first default route.

default_interface=$(route -n | awk '$1 == "0.0.0.0" ') ip_address=$(ifconfig "$default_interface" | awk 'sub(/.* inet addr:/, "") ') 

You could do an ip route get on a fairly well-known IP address which will give you the local source addr. e.g. Google's DNS server is 8.8.8.8. In one short one-liner: ip route get 8.8.8.8 | sed -nr 's/.*src ([0-9.]+).*/\1/p'

I had a look at Linux Mint's /var/lib/dhcp/ (based on Ubuntu 14.04) folder and the only file there was empty.

But searching every file, I did find:

  • /var/lib/NetworkManager/dhclient-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-eth0.lease
    (with the x's nearly random letters & numbers) with a fixed-address xxx.xxx. line that looks like the pasted file from linuxfan's answer
  • entries in /var/log/syslog with the IP address (for example here 192.168.1.2), you could pick one of these (perhaps the last one in case it changes frequently)

Jan 8 17:49:45 mint dhclient: DHCPREQUEST of 192.168.1.2 on eth0 to 255.255.255.255 port 67 (xid=0xXXXXXXXX)
Jan 8 17:49:45 mint dhclient: DHCPOFFER of 192.168.1.2 from 192.168.1.1
Jan 8 17:49:45 mint dhclient: DHCPACK of 192.168.1.2 from 192.168.1.1
Jan 8 17:49:45 mint dhclient: bound to 192.168.1.2 -- renewal in 38149 seconds.
Jan 8 17:49:45 mint NetworkManager[1363]: address 192.168.1.2
Jan 8 17:49:45 mint avahi-daemon[1117]: Joining mDNS multicast group on interface eth0.IPv4 with address 192.168.1.2.
Jan 8 17:49:45 mint avahi-daemon[1117]: Registering new address record for 192.168.1.2 on eth0.IPv4.

Or, if you're using a bash or other shell script or something shell-friendly, here's a pipe-friendly grep & cut way to get the ip from ifconfig , change eth0 to whichever you prefer, or even -a for all.

ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d ' ' -f 1 

It doesn't take much more time than grep -ing a single file, the above takes real 0m0.002s, user 0m0.000s, sys 0m0.000s while grep -ing one file takes real 0m0.001s, user 0m0.000s, sys 0m0.000s

Источник

Оцените статью
Adblock
detector