Contact us

Getting the last inactive vboxnet interface with Ruby

January 30, 2021 – Samuli Seppänen

This could possibly be the most esoteric blog post I've ever written, but here it goes. So, for reasons even we have difficulty comprehending (hint: related to PXE booting in a Vagrant/Virtualbox environment) we had to find out which inactive vboxnet (hostonly) interface had the highest index. We wanted the code to (hopefully) run on all platforms (Linux, MacOS, Windows) so we wanted to write this code in Ruby. So, this was the brutal but working end-result:

#!/usr/bin/env ruby
#
# Get the highest vboxnet interface that is inactive (down)
#
 
require 'socket'

def vboxnet_ips
  Socket.getifaddrs.select { |iface| iface.name =~ %r{vboxnet\d+} }
end

def inactive_ips(ips)
  ips.select { |iface| iface.flags & Socket::IFF_UP == 0 }
end

def iface_names(ips)
  ifaces = []
  ips.each { |ip| ifaces.append(ip.name) }
  ifaces.uniq
end

def last_inactive_iface(ifaces)
  last_index = ifaces.map { |iface| iface.sub('vboxnet', '').to_i }.sort[-1]
  "vboxnet#{last_index}"
end

p last_inactive_iface(iface_names(inactive_ips(vboxnet_ips)))

This uses the Ruby Socket library. Basically what it does is get all the IPs (not interfaces, filtering out those which are not names vboxnet*. Then it checks which of those are inactive (flag IFF_UP not set). Then it goes through the list of the objects and just gets their name (e.g. "vboxnet0") and removes duplicates. Finally the resulting unordered array of strings is converted into a list of integers, sorted and the highest index along with string "vboxnet" is given back.

This could probably be prettier, but perfect is the worst enemy of "good enough".

Did you like the article? Share it with others!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #edenred #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #json #kanban #keycloak #letsencrypt #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #microsoft #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #oidc #openshift #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #robotframework #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #twilio #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle