Search code examples
cnetwork-programmingssid

Can I get SSID and MAC address from C code in Linux?


I am writing a C backup program for my Majaro Linux. It must backup files at a certain time in my home local server only if I am connecting to my home network. So I need to get an SSID and a MAC address a current network to decide if it is my network or not.

Is there Linux(Arch) default commands, C library functions or files, contain this information?

I already tried some Linux tools, ifconfig for example, but it is useless for me.

Help!


Solution

  • Done

    Thank you all for your help, especially to Iliya Iliev and to this library. It works perfectly.

    It exactly what I've been searching for!

    I just add it to my main project.

    #include "../wifi_scan.h"
    #include <stdio.h>  
    #include <unistd.h> 
    
    const char *bssid_to_string(const uint8_t bssid[BSSID_LENGTH], char  bssid_string[BSSID_STRING_LENGTH])
        {
            snprintf(bssid_string, BSSID_STRING_LENGTH, "%02x:%02x:%02x:%02x:%02x:%02x",
                 bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
            printf("%x\n", bssid[5]);
            return bssid_string;
        }
    
        int main(int argc, char **argv){
    
            struct wifi_scan *wifi=NULL;
            struct station_info station;
            char mac[BSSID_STRING_LENGTH]; 
    
            wifi=wifi_scan_init(argv[1]);
            wifi_scan_station(wifi, &station);
    
            printf("ssid = %s mac = %s \n", station.ssid, bssid_to_string(station.bssid, mac));
    
            wifi_scan_close(wifi);
    
        }