2019独角兽企业重金招聘Python工程师标准>>>

import os
import sys
import pexpect
import re
import subprocess
import timeclass Wifi:def __init__(self):""""""self.cfg_file = '/tmp/wifi_config_file'def raiseError(self, Message=None):raise Exception, Messagedef ExcuteCMD(self, cmd):"Excute shell command ,return code and output"cmd = cmdprint 'CMD is ### ' + cmd + ' ###'rc = 1content = ""# print (datetime.datetime.now())p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)p.wait()rc = p.returncodeprint 'rc is ' + str(rc)content = p.stdout.read().strip()if content:print ">" + content + "<"if p.returncode == 0:# print 'AT_INFO : "' + cmd + '" Excute SUCESS!'rc = Trueelse:print 'return code :' + str(rc)print 'AT_WARNING : "' + cmd + '" Excute FAIL!'rc = Falsep.stdout.close()print '*'*100return rc, contentdef Get_Wlan_Card_Name(self):cmd = 'ifconfig -a |grep -o "^ *wlan[0-9][0-9]* *"|awk ' + "'{print $1}'"rc, result = self.ExcuteCMD(cmd)if rc:data = result.strip()else:data = Noneself.raiseError("error")return datadef Up_Monitor_Interface(self):wlan = self.Get_Wlan_Card_Name()mon = 'mon_' + str(wlan)if wlan:if self.ExcuteCMD(cmd='ifconfig |grep "^ *' + wlan + '"')[0]:passelse:if self.ExcuteCMD(cmd='ifconfig ' + wlan + ' up')[0]:passelse:print 'AT_ERROR : ' + 'ifconfig ' + wlan + ' up' + ' FAIL FAIL!'return False, Noneif self.ExcuteCMD(cmd='ifconfig |grep ' + mon)[0]:print 'AT_INFO : Monitor Interface is already Up PASS PASS!'return True, monelif self.ExcuteCMD(cmd='ifconfig -a|grep ' + mon)[0]:print 'AT_INFO : Monitor Interface is exist,but NOT UP!'if self.ExcuteCMD(cmd='ifconfig ' + mon + ' up')[0]:print 'AT_INFO : Monitor Interface is already Up PASS PASS!'return True, monelse:print 'AT_ERROR : Monitor Interface Up FAIL FAIL!'return False, Noneelse:if self.ExcuteCMD(cmd='iw dev ' + wlan + ' interface add ' + mon + ' type monitor')[0]:if self.ExcuteCMD(cmd='ifconfig |grep ' + mon)[0]:print 'AT_INFO : Monitor Interface is already Up PASS PASS!'return True, monelif self.ExcuteCMD(cmd='ifconfig -a|grep ' + mon)[0]:if self.ExcuteCMD(cmd='ifconfig ' + mon + ' up')[0]:print 'AT_INFO : Monitor Interface Up PASS PASS!'return True, monelse:print 'AT_ERROR : Monitor Interface Up FAIL FAIL!'return False, Noneelse:print 'AT_ERROR : Monitor Interface Add FAIL FAIL!'return False, monelse:print 'AT_ERROR : NO Exist WLAN Card!'return False, Nonedef readfile(self, file, filter=''):filter = filtermatch_flag = Falsetry:file = open(file, 'r')except Exception, e:print ereturn Falsefor line in file:print line,if filter:m = re.findall(filter, line)if m:match_flag = Truefile.close()return match_flagdef Scan_SSID(self, ssid='', bssid=''):wlan = self.Get_Wlan_Card_Name()ssid=str(ssid)bssid=str(bssid)cmd = 'iw dev ' + wlan + ' scan |grep "' + ssid +'" -8' + '|grep ' + bssidif self.ExcuteCMD(cmd)[0]:return Trueelse:print 'AT_WARNING : San_SSID RUN FAIL FAIL!'self.raiseError("error")def Start_WPASupplicant(self):wlan = self.Get_Wlan_Card_Name()os.system('killall wpa_supplicant')time.sleep(5)cmd = 'nohup wpa_supplicant -d -i '+ wlan + ' -c ' + self.cfg_file + ' 2>&1 & 'if os.system(cmd) == 0:print 'AT_INFO : Start_WPASupplicant RUN SUCCESS SUCCESS!'return Trueelse:print 'AT_WARNING : Start_WPASupplicant RUN FAIL FAIL!'self.raiseError("error")def Check_WPASupplicant(self):time.sleep(10)cmd = 'wpa_cli status'rc,result=self.ExcuteCMD(cmd)if rc:m=re.findall(r'wpa_state=COMPLETED',result)if m:print 'AT_INFO : Check_WPASuppcliant RUN SUCCESS SUCCESS! And wpa_state is COMPLETED 'return Trueelse:print 'AT_INFO : Check_WPASuppcliant RUN SUCCESS SUCCESS! But wpa_state is not COMPLETED 'self.raiseError("error")else:print 'AT_WARNING : Check_WPASuppcliant RUN FAIL FAIL!'self.raiseError("error")def Generate_Config_file(self, ssid='', bssid='', type='', key=''):"""Generate Wifi Config file for Wireless Card"""ssid=str(ssid)bssid=str(bssid)type=str(type)key=str(key)try:file = open(self.cfg_file, 'w')except Exception, e:print ereturn Falsefile.writelines('ctrl_interface=/var/run/wpa_supplicant' + os.linesep)file.writelines('eapol_version=1' + os.linesep)file.writelines('ap_scan=1' + os.linesep)file.writelines('fast_reauth=1' + os.linesep)file.writelines('network={' + os.linesep)file.writelines('    ssid=\"' + ssid + '\"' + os.linesep)file.writelines('    bssid=' + bssid  + os.linesep)file.writelines('    scan_ssid=1' + os.linesep)file.writelines('    key_mgmt=' + type + os.linesep)file.writelines('    priority=5' + os.linesep)file.writelines('    pairwise=CCMP' + os.linesep)file.writelines('    proto=WPA RSN' + os.linesep)file.writelines('    psk=\"' + key + '\"' + os.linesep)file.writelines('}' + os.linesep)file.close()self.readfile(self.cfg_file)return Truefile.close()self.readfile(self.cfg_file)return Truedef connect_SSID(self, ssid='', bssid='', type='', key=''):"""check Wireless Card can connect Wireless AP or not-Give this Keyword the 4 arguments of a AP: ssid, bssid, type and key-Then Case Word will scan SSID Exist or not ,and generate a Wifi config fileIf  Scan SSID Succeed  and config file is Wright, it will invoke wirelessTool wpa_supplicant to connect the AP and check the wireless status withWireless Tool wpa_cli.-When wpa_state is "COMPLETED", the Keyword run succeed, otherwise it fails.Examples:| connect SSID | bssid=10:9f:a9:70:01:03 | type=WPA-PSK   |   key=1234567890   |"""ssid=str(ssid)bssid=str(bssid)type=str(type)key=str(key)if self.Up_Monitor_Interface():passelse:self.raiseError("error")if self.Generate_Config_file(ssid=ssid, bssid=bssid, type=type,  key=key):passelse:self.raiseError("error")if self.Scan_SSID(ssid=ssid, bssid=bssid):print "scan ssid pass"passelse:print "scan ssid false"self.raiseError("error")if self.Start_WPASupplicant():passelse:self.raiseError("error")if self.Check_WPASupplicant():passelse:self.raiseError("error")

转载于:https://my.oschina.net/xxjbs001/blog/304884

RobotFramework Library相关推荐

  1. RobotFramework全部内置库及第三方库介绍,以及安装方法

    RobotFramework全部内置标准库及第三方库介绍,以及安装方法 1.Libraries 1.1 Standard Libraries 内置标准库 1.2 External Libraries ...

  2. Robotframework+selenium+ride自动化框架搭建(Mac)

    前段时间在学习robot framework + selenium + ride自动化测试框架,今天腾出一些时间总结一下学习该框架的第一步-框架的搭建,对该框架的使用总结持续进行.因为我使用的是Mac ...

  3. robotframework安装包_Robot Framework零基础入门教程

    原作者:逆之時間軸 原出处:CSDN 原文链接:https://blog.csdn.net/yydoraemon/article/details/13847781 版权声明:本文为博主原创文章,转载请 ...

  4. Robot Framework 自动化框架 - 定制自己的library

    Robot 自动化框架内置提供了一些library,如OperatingSystem(包含一些常用的的文件操作关键字,如copy文件,创建目录),Telent,Screenshot,String,另外 ...

  5. Appium+RobotFrameWork测试环境搭建

    前提:搭建好robotframework环境 RF基于python2.7的版本实现的一套开源自动化测试框架 推荐使用Appium Desktop, 搭建Appium环境: 1. 搭建JDK 2. 搭建 ...

  6. RobotFramework读取mysql和oracle数据库

    一.robotframework连接mysql数据库 1.安装databaselibrary.pymysql 通过cmd命令执行:pip install robotframework-database ...

  7. RobotFramework自动化框架—数据驱动测试

    前情介绍: 在自动化测试框架中,数据驱动的意思是指定的是测试用例或者说测试套件是由外部数据集合来驱动的框架. 数据集合(也可称之为数据来源)可以是任何类型的数据文件比如xls,xlsx,csv等等,甚 ...

  8. RobotFramework系列(二):Robot 如何编写自定义关键字

    今天尝试在RF中使用一下自己写的关键字. 1.首先写一个py文件,如下,简单打印个message 2.在RF中点击library,把写的py文件加进来 3.使用函数mylog,有一个参数,也可以F5看 ...

  9. RobotFramework系列(一):RIDE使用教程

    刚接触自动化测试不久,对里边的各种技术还是挺感兴趣的,所以也学着那些测试大牛们编写个自动化测试脚本,体验一下自动化的感觉. 一.Robot Framework简介 1)RobotFramework   ...

最新文章

  1. css字体居中_简单介绍CSS.
  2. 安装DNN4.6.2 source code出现的问题
  3. linux二进制文件构建mysql_linux上二进制部署mysql详细步骤(测试环境常用)
  4. vue前端架构文档_Dubbo分布式架构搭建教育PC站-Vue前端:课程和登录页
  5. ZooKeeper学习-- Zookeeper简单介绍
  6. C++ 为什么要用覆盖(学习笔记)
  7. 记录pageHelper分页orderby的坑
  8. 使用TensorFlow.js的AI聊天机器人三:改进了文本中的情感检测
  9. 三菱PLC编程线缆制作
  10. 遍历~筛选~eq();filter();first();last();has();is();map();slice()
  11. 怒爬某 Hub 资源就为撸了一个鉴黄平台
  12. centos7显示时间的时区修改
  13. 如何通过Dropbox访问iPad上的电子书库
  14. 备案后可以改服务器信息吗,域名备案后可以更改服务器
  15. 2022年最新版Android安卓面试题+答案精选(每日20题,持续更新中)【八】
  16. 深耕MySQL - 如何使用联合索引执行查询
  17. 基于原生JS项目使用Vue3 + Surely Vue Table组件
  18. Spring Boot应用退出
  19. centos系统中php Curl 无法访问https 的解决办法
  20. FFmpeg —— 编译obs源码(解决编译后不能捕获摄像头问题)

热门文章

  1. 计算圆弧与矩形相交_【技术】新型七层矩形卷边工艺实践
  2. idea2020.01的git下的Local Changes消失的问题
  3. Spark基础学习笔记13:Scala函数
  4. Java案例:生成指定目录下某种类型文件的列表
  5. 【codevs3044】矩形面积求并【POJ1151】Atlantis,第一次的扫描线
  6. livecharts中仪表盘_Vue中使用Echarts仪表盘展示实时数据的实现
  7. 杨辉三角、组合数 性质的探究
  8. 【英语学习】【English L06】U02 Food L1 Food on the menu
  9. 【英语学习】【WOTD】prestigious 释义/词源/示例
  10. sentinel卫星_IKONOS卫星 遥感影像解译数据 波段