Hemanth's Scribes

node

Programmatically Accessing Network Interfaces

Author Photo

Hemanth HM

Thumbnail

How to programmatically access network interfaces in different languages.

Ruby 2.1

require 'socket'
Socket.getifaddrs.each do |i|
  puts "#{i.name}: #{i.addr.ip_address}" if i.addr.ip?
end

## Python
python
import netifaces
for face in netifaces.interfaces():
    print netifaces.ifaddresses(face)

## Perl
perl
my %addresses = map {
    ($_ => [
        map { Net::Interface::inet_ntoa($_) }
        $_->address,
    ]);
} Net::Interface->interfaces;
print %addresses;

## Node.js
javascript
var os = require('os');
console.log(os.networkInterfaces());

Nothing beats ip addr show but these are handy for programming!

#node#ruby#python#cli
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.