본문 바로가기

카테고리 없음

python으로 wifi(와이파이) 정보 가져오기

 

쓰는 사람이 있을지 모르겠다

 

개발한거 누군가 보라고 올린다.

 

import os


def get_wifi_list():
    wifi_string = os.popen('iwlist scanning').read()
    wifi_string_list = wifi_string.split("Cell")[1:]
    print("Connect available wifi length : {}".format(len(wifi_string_list)))


    # get wifi infos
    wifi_dict_list = []
    wifi_index = 0
    for wifi_string in wifi_string_list :
        wifi_index += 1 # increase index
        
        wifi_string = wifi_string.replace(' ','')

        wifi_dict = {}
        line_index = 0
        for line in wifi_string.split('\n'):
            key, value = None, None
            
            if len(line) == 0 :
                continue

            if 'Address' in line : # just for first time
                # get key and wifi 
                wifi_index, key =  line.split(':')[0].split('-')
                wifi_index = int(wifi_index)
                wifi_dict['index'] = wifi_index

                # get value
                value = line.split(':')[1:]
                address_string = value[0]
                for i in range(1,len(value)):
                    address_string = "{}:{}".format(address_string, value[i])
                value = address_string
            elif 'Quality' in line :
                _, quality_value, signal_value = line.split('=')
                quality_value = quality_value.split('Signal')[0]
                wifi_dict["Quality"] = quality_value
                wifi_dict["Signallevel"] = signal_value
            elif "Unknown" in line :
                wifi_dict["IE:Unknown"] = line.split(':')[-1]
            else :
                key, value = line.split(':')

            if key is not None :
                wifi_dict[key] = value
            
            line_index += 1
        wifi_dict_list.append(wifi_dict)
    return wifi_dict_list

if __name__ == "__main__":
    wifi_dict_list = get_wifi_list()