Gambas Documentation - /snippets/getactivenetworkinterface

/snippets/getactivenetworkinterface

# Get the active network interface

Author: Dimitrios Anogiatis

This function will give you the active network interface (ie eth0 or wlan0 etc)
It can be used in shell functions or any internal function that depends on this
information

    PUBLIC FUNCTION getActiveNetworkInterface() AS String
    DIM tmpNIF AS String[]
    DIM tmpActive, tmpString AS String
    DIM iterator AS Integer
    DIM tmpFile AS File

    'get the interface directories
    tmpNIF = Dir("/sys/class/net")

    'check each folder's oper file to see if the interface is up and running
    FOR iterator = 0 TO tmpNIF.Count - 1
     tmpString = "/sys/class/net" &/ tmpNIF [iterator]&/ "operstate"

     'check the operational state of each interface

    tmpFile = OPEN tmpString FOR INPUT
      WHILE NOT Eof(tmpFile)
        LINE INPUT #tmpFile, tmpActive
      WEND
     CLOSE #tmpFile

      'if it's reported as up then return it's name
      IF tmpActive = "up" THEN RETURN tmpNIF[iterator]
    NEXT
    END