def get_wireless(): wifi = PyWiFi() faces = wifi.interfaces() if not faces: print("此电脑未安装无线网卡.") return # face[0].scan() 扫描附近的无线信号 wireless = faces[0].scan_results() print(wireless) for data in wireless: print("-" * 20) print(data.ssid) # 无线信号名称 print(AKMS[data.akm[0]]) # 加密方式 print("-" * 20)
def test_connect(wifi_name,wifi_password): wifi = PyWiFi() iface = wifi.interfaces()[0] test_disconnect() time.sleep(3) profile_info = Profile() profile_info.ssid = wifi_name profile_info.auth = const.AUTH_ALG_OPEN profile_info.akm.append(const.AKM_TYPE_NONE) profile_info.cipher = const.CIPHER_TYPE_CCMP iface.remove_all_network_profiles() tmp_profile = iface.add_network_profile(profile_info) iface.connect(tmp_profile) time.sleep(5) if iface.status() == const.IFACE_CONNECTED: print("wifi: %s 连接成功"% wifi_name) else: print("wifi: %s 连接失败" % wifi_name)
def test_scan(): wifi = PyWiFi() # 创建一个无线对象 iface = wifi.interfaces()[0] # 取一个无限网卡 iface.scan() # 扫描s # (必不可少)这个sleep时间必须有,不知道为啥。 time.sleep(0.5) wifi_information = { 'ABCD': [-200, 123456789], 'hello_world': [-200, 850898840] } result = iface.scan_results() for i in range(len(result)): if result[i].ssid in wifi_information.keys(): wifi_information[result[i].ssid] = [ result[i].signal, wifi_information[result[i].ssid][1] ] # time.sleep(1) return wifi_information
def get_wifi_interface(): wifi = PyWiFi() if len(wifi.interfaces()) <= 0: print u'未找到无线网卡接口!' exit() if len(wifi.interfaces()) == 1: print u'无线网卡接口: %s' % (wifi.interfaces()[0].name()) return wifi.interfaces()[0] else: print '%-4s %s' % (u'序号', u'网卡接口名称') for i, w in enumerate(wifi.interfaces()): print '%-4s %s' % (i, w.name()) while True: iface_no = raw_input('请选择网卡接口序号:'.decode('utf-8').encode('gbk')) no = int(iface_no) if no >= 0 and no < len(wifi.interfaces()): return wifi.interfaces()[no]
def get_wifi_interface(): wifi = PyWiFi() if len(wifi.interfaces()) <= 0: sys.exit('No wifi inteface found!') print("=" * 73) if len(wifi.interfaces()) == 1: print(O + 'Wifi interface found:' + C + wifi.interfaces()[0].name() + W) return wifi.interfaces()[0] else: print('%-4s %s' % ('No', 'interface name')) for i, w in enumerate(wifi.interfaces()): print('%-4s %s' % (i, w.name())) while True: iface_no = input('Please choose interface No:') no = int(iface_no) if no >= 0 and no < len(wifi.interfaces()): return wifi.interfaces()[no]
def main(): # 扫描时长 scantimes = 3 # 单个密码测试延迟 testtimes = 15 output = sys.stdout # 结果文件保存路径 files = "TestRes.txt" # 字典列表 super_keys file_path = os.path.abspath('../../../test.txt') print file_path # 密码编号 pwd_num = 0 with open(file_path, 'r') as f: for key in f: if not key: break pwd_num = pwd_num + 1 # keys = open(file_path, "r").readlines() # print"|KEYS %s" % (len(keys)) # 实例化一个pywifi对象 wifi = PyWiFi() # 选择定一个网卡并赋值于iface iface = wifi.interfaces()[0] # 通过iface进行一个时长为scantimes的扫描并获取附近的热点基础配置 scanres = scans(iface, scantimes) # 统计附近被发现的热点数量 nums = len(scanres) print "|SCAN GET {0}".format(nums) print "%s\n%-*s|%-*s|%-*s|%-*s|%-*s|%-*s%*s\n%s" % ( "-" * 70, 6, "WIFIID", 18, "SSID OR BSSID", 2, "N", 4, "time", 7, "signal", 10, "KEYNUM", 10, "KEY", "=" * 70) # 将每一个热点信息逐一进行测试 for i, x in enumerate(scanres): # 测试完毕后,成功的结果讲存储到files中 res = test(nums - i, iface, x, pwd_num, key, output, testtimes) # res = test(nums - i, iface, x, key, output, testtimes) print res if res: open(files, "a").write(res)
def main(): try: #捕获异常处理,如果执行脚本不是root,就抛出错误 if os.getuid() != 0: print("[-] Run me as root") exit(1) except Exception as msg: print(msg) wifi = PyWiFi() #创建并且选择一张网卡 iface = wifi.interfaces()[0] print("当前已选择网卡: %s" % iface.name()) print('工具启动,正在扫描可用WIFI……') wifi_list = scan_ap(iface) #输出一个扫描结果wifi表 print("扫描完成!") while (1): print('--------------------------') print('请选择需要的服务:') print('1.显示可用WIFI列表') print('2.再次扫描') print('3.选择连入WIFI') print('4.查看当前网卡状态') print('5.断开当前网卡连接') print('6.退出程序') print('--------------------------') choice = input() if (choice == '1'): show_wifi_list(wifi_list) elif (choice == '2'): '''再次扫描,得到结果''' print('正在扫描……') wifi_list = scan_ap(iface) print("扫描完成!") elif (choice == '3'): connect_ap(iface, wifi_list) elif (choice == '4'): interfaces_status(iface) elif (choice == '5'): interface_disconnect(iface) elif (choice == '6'): exit(0)
def test1(wifi_name, wifi_password): wifi = PyWiFi() iface = wifi.interfaces()[0] iface.disconnect() time.sleep(1) info = Profile() info.ssid = wifi_name info.auth = const.AUTH_ALG_OPEN info.akm.append(const.AKM_TYPE_WPA2PSK) info.cipher = const.CIPHER_TYPE_CCMP info.key = wifi_password iface.remove_all_network_profiles() tmp = iface.add_network_profile(info) iface.connect(tmp) time.sleep(3) if iface.status() == const.IFACE_CONNECTED: print('成功') print(wifi_password) else: print('失败')
def connect_wifi(ssid, password): # function to connect to Wi-Fi with Password wifi = PyWiFi() iface = wifi.interfaces()[0] iface.disconnect() # disconnects from the current Wi-Fi if any time.sleep(2) profile = Profile() # adding new Profile for given Wi-Fi profile.ssid = ssid profile.auth = const.AUTH_ALG_OPEN profile.akm.append(const.AKM_TYPE_WPA2PSK) profile.cipher = const.CIPHER_TYPE_CCMP profile.key = password print("\nConnecting to WiFi....") iface.connect(iface.add_network_profile(profile)) time.sleep(5) if iface.status() == const.IFACE_CONNECTED: print("\t>> Connected Successfully!") else: print("\t>> Failed to Connect!")
def run(self): try: wifi = PyWiFi() # 创建一个wifi对象 iface = wifi.interfaces()[0] # 取第一个无限网卡 iface.disconnect() # 断开网卡连接 profile = Profile() # 配置文件 profile.ssid = "Shu(ForAll)" # wifi名称 iface.remove_all_network_profiles() # 删除其他配置文件 tmp_profile = iface.add_network_profile(profile) # 加载配置文件 iface.connect(tmp_profile) # 连接 sleep(1) # 不延时,wifi反应不过来 s0 = "Shu(ForAll) 连接成功\n" shu = shuConnect(self.user, self.passwd, chose=2) s = s0 + shu.start_connect() except: s = 'macError' self.signalCon.emit(s)
def connect(wifi_name, wifi_password, auth_type): '''Connect WiFi''' wifi = PyWiFi() iface = wifi.interfaces()[0] iface.disconnect() time.sleep(1) profile_info = Profile() profile_info.ssid = wifi_name profile_info.auth = const.AUTH_ALG_OPEN if auth_type == "OPEN": profile_info.akm.append(const.AKM_TYPE_NONE) profile_info.cipher = const.CIPHER_TYPE_NONE else: profile_info.akm.append(const.AKM_TYPE_WPA2PSK) profile_info.cipher = const.CIPHER_TYPE_CCMP profile_info.key = wifi_password iface.remove_all_network_profiles() tmp_profile = iface.add_network_profile(profile_info) iface.connect(tmp_profile) time.sleep(1) if iface.status() == const.IFACE_CONNECTED: print("WiFi: %s 连接成功" % wifi_name) else: print("WiFi: %s 连接失败" % wifi_name)
def connect_wifi(wifi_name, wifi_password): wifi = PyWiFi() # 创建一个无限对象 ifaces = wifi.interfaces()[0] # 取一个无限网卡 # print(ifaces.name()) # 输出无线网卡名称 ifaces.disconnect() # 断开网卡连接 # time.sleep(1) # 缓冲3秒 profile_info = Profile() # 配置文件 profile_info.ssid = wifi_name # wifi名称 profile_info.auth = const.AUTH_ALG_OPEN # 需要密码 profile_info.akm.append(const.AKM_TYPE_WPA2PSK) # 加密类型 profile_info.cipher = const.CIPHER_TYPE_CCMP # 加密单元 profile_info.key = wifi_password ifaces.remove_all_network_profiles() # 删除其他配置文件 tmp_profile = ifaces.add_network_profile(profile_info) # 加载配置文件 ifaces.connect(tmp_profile) # 连接 time.sleep(2) # (必不可少)尝试2秒能否成功连接 isok = True if ifaces.status() == const.IFACE_CONNECTED: print("成功切换连接为wifi: %s" % wifi_name) else: print("wifi: %s 连接失败" % wifi_name) # time.sleep(2) return isok
class wifi(object): def __init__(self): self.wifi = PyWiFi() #创建一个无线对象 self.interfaces = self.wifi.interfaces() #获取无线网卡接口 self.iface = self.interfaces[0] #获取第一个无线网卡接口 # 获取无线网卡接口 def get_wifi_interfaces(self): num = len(self.interfaces) if num <= 0: print(u'未找到无线网卡接口!\n') exit() if num == 1: print(u'无线网卡接口: %s\n' % (self.iface.name())) return self.iface else: print('%-4s %s\n' % (u'序号', u'网卡接口名称')) for i, w in enumerate(self.interfaces): print('%-4s %s' % (i, w.name())) while True: iface_no = input('请选择网卡接口序号:'.decode('utf-8').encode('gbk')) no = int(iface_no) if no >= 0 and no < num: return self.interfaces[no] # 查看无线网卡是否处于连接状态 def check_interfaces(self): if self.iface.status() in [ const.IFACE_CONNECTED, const.IFACE_CONNECTING ]: print('无线网卡:%s 已连接。' % self.iface.name()) else: print('无线网卡:%s 未连接。' % self.iface.name()) # 扫描周围wifi def scan_wifi(self): self.iface.scan() #扫描周围wifi time.sleep(1) #不缓冲显示不出来 result = self.iface.scan_results() #获取扫描结果,wifi可能会有重复 has = [] #初始化已扫描到的wifi wifi_list = [] #初始化扫描结果 for i in result: if i not in has: #若has中没有该wifi,则 has.append(i) #添加到has列表 if i.signal > -90: #信号强度<-90的wifi几乎连不上 wifi_list.append((i.ssid, i.signal)) #添加到wifi列表 print('wifi信号强度:{0},名称:{1}。'.format(i.signal, i.ssid)) #输出wifi名称 return sorted(wifi_list, key=lambda x: x[1], reverse=True) #按信号强度由高到低排序 # 连接wifi def connect_wifi(self, wifi_name, wifi_password): self.iface.disconnect() #断开无线网卡连接 time.sleep(1) #缓冲1秒 profile_info = Profile() #wifi配置文件 profile_info.ssid = wifi_name #wifi名称 profile_info.auth = const.AUTH_ALG_OPEN #需要密码 profile_info.akm.append(const.AKM_TYPE_WPA2PSK) #加密类型 profile_info.cipher = const.CIPHER_TYPE_CCMP #加密单元 profile_info.key = wifi_password #wifi密码 self.iface.remove_all_network_profiles() #删除其他配置文件 tmp_profile = self.iface.add_network_profile(profile_info) #加载配置文件 self.iface.connect(tmp_profile) #连接 #尝试5秒是否能成功连接(时间过短可能会导致正确密码尚未连接成功) time.sleep(5) if self.iface.status() == const.IFACE_CONNECTED: print( '\n==========================================================================' ) print('wifi:{0}连接成功,密码:{1}'.format(wifi_name, wifi_password), end='') print( '==========================================================================\n' ) return True else: print('密码错误:{0}'.format(wifi_password), end='') return False # 断开无线网卡已连接状态 def disconnect_wifi(self): self.iface.disconnect() if self.iface.status() in [ const.IFACE_DISCONNECTED, const.IFACE_INACTIVE ]: print('无线网卡:%s 已断开。' % self.iface.name()) else: print('无线网卡:%s 未断开。' % self.iface.name())
import sys import os import os.path import platform import re import time import pywifi from pywifi import PyWiFi from pywifi import const from pywifi import Profile try: # wlan wifi = PyWiFi() ifaces = wifi.interfaces()[0] ifaces.scan() #check the card results = ifaces.scan_results() wifi = pywifi.PyWiFi() iface = wifi.interfaces()[0] except: print("[-] Error system") type = False def main(ssid, password): profile = Profile()
def get_wireless(): wifi = PyWiFi() iface = wifi.interfaces()[0] for i in iface.scan_results(): print(i.ssid, i.akm[0])
class wifi(object): def __init__(self): self.wifi = PyWiFi() # 创建一个无线对象 self.interfaces = self.wifi.interfaces() # 获取无线网卡接口 self.iface = self.interfaces[-1] # 获取第一个无线网卡接口 # self.iface1 = self.interfaces[-1] # 获取无线网卡接口 def get_wifi_interfaces(self): num = len(self.interfaces) if num <= 0: print(u'未找到无线网卡接口!') exit() if num == 1: print(u'无线网卡接口: %s' % (self.iface.name())) return self.iface else: print('%-4s %s\n' % (u'序号', u'网卡接口名称')) for i, w in enumerate(self.interfaces): print('%-4s %s' % (i, w.name())) while True: # iface_no = input('请选择网卡接口序号:') iface_no = 1 # iface_no = input('请选择网卡接口序号:'.decode('utf-8').encode('gbk'))1 no = int(iface_no) if no >= 0 and no < num: return self.interfaces[no] # 查看无线网卡是否处于连接状态 def check_interfaces(self): if self.iface.status() in [ const.IFACE_CONNECTED, const.IFACE_CONNECTING ]: print('无线网卡:%s 已连接。' % self.iface.name()) return True else: print('无线网卡:%s 未连接。' % self.iface.name()) return False # 扫描周围wifi def scan_wifi(self): self.iface.scan() # 扫描周围wifi time.sleep(1) # 不缓冲显示不出来 result = self.iface.scan_results() # 获取扫描结果,wifi可能会有重复 has = [] # 初始化已扫描到的wifi wifi_list = [] # 初始化扫描结果 for i in result: if i not in has: # 若has中没有该wifi,则 has.append(i) # 添加到has列表 if i.signal > -90: # 信号强度<-90的wifi几乎连不上 DUOwifi = re.match(r'^(BusMovie\.net_)(\d{4}$)', i.ssid) if DUOwifi: Sn = DUOwifi[2] DUOSN = 'ND301802011{0}'.format(Sn) wifi_list.append( (i.ssid, i.signal, DUOSN)) # 添加到wifi列表 print('名称:{0},wifi信号强度:{1},SN:ND301802011{2}'.format( i.ssid, i.signal, Sn)) # 输出wifi名称 # return sorted(wifi_list, key=lambda x: x[1], reverse=True) # 按信号强度由高到低排序 return sorted(wifi_list, key=lambda x: x[2]) # 按SNc从小到大排序 # 连接wifi def connect_wifi(self, wifi_name): self.iface.disconnect() # 断开无线网卡连接 time.sleep(1) # 缓冲1秒 profile_info = Profile() # wifi配置文件 profile_info.ssid = wifi_name # wifi名称 # profile_info.auth = const.AUTH_ALG_OPEN # 需要密码 profile_info.akm.append(const.AKM_TYPE_NONE) # 加密类型 profile_info.cipher = const.CIPHER_TYPE_NONE # 加密单元 # profile_info.key = wifi_password # wifi密码 self.iface.remove_all_network_profiles() # 删除其他配置文件 tmp_profile = self.iface.add_network_profile(profile_info) # 加载配置文件 self.iface.connect(tmp_profile) # 连接 # 尝试5秒是否能成功连接(时间过短可能会导致正确密码尚未连接成功) time.sleep(5) if self.iface.status() == const.IFACE_CONNECTED: print( '==========================================================================' ) print('wifi:{0} 连接成功'.format(wifi_name), end='\n') print( '==========================================================================' ) return True else: print('wifi:{0}连接失败'.format(wifi_name), end='') return False # 断开无线网卡已连接状态 def disconnect_wifi(self): self.iface.disconnect() if self.iface.status() in [ const.IFACE_DISCONNECTED, const.IFACE_INACTIVE ]: print('无线网卡:%s 已断开。' % self.iface.name()) else: print('无线网卡:%s 未断开。' % self.iface.name()) def prired(self, *text): text = str(text) ll = '\033[1;31;46m' + text + '\033[0m' print(ll)
class LinkWifi(): def __init__(self): self.wifi = PyWiFi() self.iface = self.wifi.interfaces()[0] self.akm = { 'None': const.AKM_TYPE_NONE, 'WPA': const.AKM_TYPE_WPAPSK, 'WPA2': const.AKM_TYPE_WPA2PSK, } def check_wifi_connected(self, timeout=5): max_try_times = timeout // 0.5 cur_try_times = 0 while True: status = self.iface.status() if status in [const.IFACE_INACTIVE, const.IFACE_CONNECTED]: return status else: if cur_try_times >= max_try_times: break time.sleep(0.5) cur_try_times += 1 return const.IFACE_DISCONNECTED @staticmethod def status(status): if status == "disconnected": return const.IFACE_DISCONNECTED elif status == "scanning": return const.IFACE_SCANNING elif status == "inactive": return const.IFACE_INACTIVE elif status == "connecting": return const.IFACE_CONNECTING elif status == "connected": return const.IFACE_CONNECTED else: return -1 def connect_wifi(self, wifi_name, wifi_key=None, akm='WPA2', check_connected=True): assert akm in ['None', 'WPA', 'WPA2'] profile = Profile() # The ssid of the AP(Authentication Profile). profile.ssid = wifi_name # The authentication algorithm of the AP. For normal case, almost all the APs use open algorithm. profile.auth = const.AUTH_ALG_OPEN # The cipher type of the AP. The cipher type should be set to the Profile if the akm is not AKM_TYPE_NONE. cipher = const.CIPHER_TYPE_CCMP if wifi_key == None: akm = 'None' cipher = const.CIPHER_TYPE_NONE else: profile.key = wifi_key profile.cipher = cipher # The key management type of the AP. profile.akm.append(self.akm[akm]) profile = self.iface.add_network_profile(profile) self.iface.connect(profile) if check_connected: return self.check_wifi_connected() else: return True def disconnect_wifi(self): is_connected = self.check_wifi_connected(timeout=1) if is_connected: # Disconnect current AP connection. self.iface.disconnect() def get_wifi_list(self, time_out=8, no_wait=True): # Trigger the interface to scan APs. self.iface.scan() max_try_times = time_out // 0.5 cur_try_times = 0 if no_wait: while True: profiles = self.__get_scan_result__(only_name=False) if len(profiles) > 0: return profiles else: if cur_try_times >= max_try_times: break time.sleep(0.5) cur_try_times += 1 return None else: time.sleep(time_out) return self.__get_scan_result__(only_name=False) def check_wifi_in_nearby(self, wifi_name, time_out=8): self.iface.scan() max_try_times = time_out * 2 for i in range(max_try_times): time.sleep(0.5) # print(self.__get_scan_result__()) if wifi_name in self.__get_scan_result__(): return True return False def __get_scan_result__(self, only_name=True): wifi_name_list = [] wifi_list = [] profiles = self.iface.scan_results() if profiles is not None: for p in profiles: if p.ssid not in wifi_name_list: wifi_list.append(p) wifi_name_list.append(p.ssid) if only_name: return wifi_name_list return wifi_list
####### # Setting the color combinations RED = "\033[1;31m" BLUE = "\033[1;34m" CYAN = "\033[1;36m" GREEN = "\033[0;32m" RESET = "\033[0;0m" BOLD = "\033[;1m" REVERSE = "\033[;7m" try: # Interface information wifi = PyWiFi() ifaces = wifi.interfaces()[0] # for wifi we use index - 0 ifaces.scan() #check the card results = ifaces.scan_results( ) #Obtain the results of the previous triggerred scan. A Profile list will be returned. wifi = pywifi.PyWiFi( ) # A Profile is the settings of the AP we want to connect to iface = wifi.interfaces()[0] except: print("[-] Error system") type = False
class WiFi(object): # 创建对象自动初始化,类似Java的构造函数 def __init__(self): self.wifi = PyWiFi() # 创建一个无线对象 self.iface = self.wifi.interfaces()[0] # 获取当前机器第一个无线网卡 # 查看wifi的连接状态 def wifi_connect_status(self): """ 判断本机是否有无线网卡,以及连接状态 :return:已连接或存在网卡返回1,否则返回0 """ ret_list = [] # 判断是否连接成功 if self.iface.status() in \ [const.IFACE_CONNECTED, const.IFACE_CONNECTING, const.IFACE_INACTIVE]: return self.iface.name() # 连接成功显示连接设备 else: return "not connected!" # 连接失败返回失败信息 """ 扫描附近wifi 乱码问题: 把wifi_info.ssid重新编码为gb18030 wifi_info.ssid.encode('raw_unicode_escape','strict').decode('gb18030') 我也不清楚他为什么不全用unicode ssid出来应该是unicode 但是 你往profile里面写的时候 必须是gb18030 所以这么一个操作 你会发现gb18030在控制台和py的某些控件上输出是乱码 是因为 控制台是utf-8 想在这上面输出中文的话你得encode('raw_unicode_escape','strict').decode() """ def scan_wifi(self, scantime=3): """ :param scantime: 指定扫描时间,默认扫描时间为5秒 :return: 返回的是一个network dictionary,key=bssid,value=ssid """ self.iface.scan() # 扫描附近wifi time.sleep(scantime) basewifi = self.iface.scan_results() # print("has end step2") dict = {} for i in basewifi: dict[i.bssid] = \ i.ssid.encode(encoding='raw_unicode_escape', errors='strict').decode() # print("has end step3") return dict def get_wifi_pass(self, wifi_ssid): tmp = wifi_ssid # tmp = hashlib.md5(tmp.encode('latin1')).hexdigest() for i in range(3): tmp = tmp + '\n' md5_obj = hashlib.md5() # need to every time new object md5_obj.update(tmp.encode('utf-8')) tmp = md5_obj.hexdigest() wifi_pass = tmp[0:7] root_pass = tmp[26:32] print(tmp,wifi_pass,root_pass) return wifi_pass, root_pass # 链接到指定wifi def connect_wifi(self, wifi_ssid, password): profile = Profile() # 配置文件 profile.ssid = wifi_ssid # wifi名称 profile.auth = const.AUTH_ALG_OPEN # 需要密码 profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密类型 profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元 profile.key = password # wifi密码 self.iface.remove_all_network_profiles() # 删除其他配置 tmp_profile = self.iface.add_network_profile(profile) # 加载配置 self.iface.connect(tmp_profile) # link start time.sleep(10) # 尝试10s是否成功 isok = True if self.iface.status() == const.IFACE_CONNECTED: return isok # 连接成功 else: isok = False # 连接失败设置isok = False self.iface.disconnect() # 避免超时后连接成功手动断开一下,因为在一定时间内连接失败用户会继续重试连接 time.sleep(1) return isok def Get_Param(self): yamlPath = "config.xml" f = open(yamlPath, 'r', encoding='utf-8')