def audit_mysql_search(self, host='', table='', expect=None, searchchar=None, username=dut_user, passwd=dut_passwd): ''' table=file_transfer,app_others,email,instant_message,online_shopping,search-enine,social_network ''' print("run keyword:%s" % (sys._getframe().f_code.co_name)) host = _unicode_to_utf(host) table = _unicode_to_utf(table) expect = _unicode_to_utf(expect) searchchar = _unicode_to_utf(searchchar) username = _unicode_to_utf(username) passwd = _unicode_to_utf(passwd) tl, msgs = self._login_diagnose(host, username, passwd) table_name = self._find_current_table(tl, table, msgs) tl.write("echo \"use syslog; select * from " + table_name + "; \" | mysql\n") msg = tl.read_until('#', self.timeout) msgs.append(msg) tl.close() msgs = ' '.join(msgs) print("%s" % msgs) _searchchar('ERR', msg, 0, 'sql') _searchchar(searchchar, msg, expect, 'sql')
def telnet_ros_run(self,host='',commands='',expect=None,searchchar=None,username=ros_user,passwd=ros_passwd): host=_unicode_to_utf(host) commands=_unicode_to_utf(commands) expect=_unicode_to_utf(expect) searchchar=_unicode_to_utf(searchchar) username=_unicode_to_utf(username) passwd=_unicode_to_utf(passwd) print("run keyword:%s ***************** host=%s"%(sys._getframe().f_code.co_name,host)) tl=telnetlib.Telnet(host,23,10) tl.read_until("Login:"******"Password:"******"%s"%cmds) for cmd in cmds: print("cmd=%s"%cmd) tl.write(cmd+'\r\n') if 'print' in cmd : show_msg=tl.read_until('>',10) msgs.append(show_msg) else : msg=tl.read_until('>',10) msgs.append(msg) tl.write('\r\n') time.sleep(1) msgs=' '.join(msgs) print("%s"%msgs) tl.close() if 'show_msg' in locals().keys() : _searchchar(searchchar,show_msg,expect,'show') else: _searchchar(None,msgs,expect)
def win_show_ip(self, inter="test", searchip=None): print("run keyword:%s" % (sys._getframe().f_code.co_name)) msgs = [] cmd = 'netsh interface ip show address "' + inter + '"' p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) p.wait() msgs.append(p.stdout.read()) msgs.append(p.stderr.read()) p.terminate() reobj = re.compile("^[ ]$") if reobj.search(msgs[1]) != None: raise ExpectError("show interface %s is fail" % inter) msg = "\n".join(msgs) print("%s" % unicode(msg, global_encoding)) _searchchar(searchip, msg, expect=1, tpye="pc")
def http_cli(self,host='',port=None,url='/',act='GET',expect=1,searchc=None): ''' ''' host=_unicode_to_utf(host) port=_unicode_to_utf(port) if (not port) or port == "None" or port.isspace(): port=None url=_unicode_to_utf(url) act=_unicode_to_utf(act) expect=_unicode_to_utf(expect) searchc=_unicode_to_utf(searchc) print("run keyword:%s"%(sys._getframe().f_code.co_name)) sendheader={"Accept-Encoding":"gzip, deflate, sdch",\ "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",\ #"Content-Length":"100",\ "Connection":"keep-alive",\ "Accept-Language":"zh-CN,zh;q=0.8",\ "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"} try: conn=httplib.HTTPConnection(host=host,port=port)#,timeout=10) conn.request(act,url,'',sendheader) msgs=conn.getresponse() body=msgs.read() conn.close() except: if expect == 0 or expect == '0': if searchc ==None or str(searchc) == 'None' or searchc == '': print("expect get url from host:%s fail, actually fail"%host) else : raise ExpectError("expect get url from host:%s ,connect fail"%host) else : raise ExpectError("get url from host:%s error!"%host) else: if searchc ==None or str(searchc) == 'None' or searchc == '' : if expect == 0 : raise ExpectError("expect get url from host:%s fail, actually success"%host) else : print("body:\n%s"%body) print("expect get url from host:%s success, actually sucess"%host) else : print("body:\n%s"%body) _searchchar(searchc,body,expect,'http')
def telnet_ros_run(self, host='', commands='', expect=None, searchchar=None, username=ros_user, passwd=ros_passwd): host = _unicode_to_utf(host) commands = _unicode_to_utf(commands) expect = _unicode_to_utf(expect) searchchar = _unicode_to_utf(searchchar) username = _unicode_to_utf(username) passwd = _unicode_to_utf(passwd) print("run keyword:%s ***************** host=%s" % (sys._getframe().f_code.co_name, host)) tl = telnetlib.Telnet(host, 23, 10) tl.read_until("Login:"******"Password:"******"%s" % cmds) for cmd in cmds: print("cmd=%s" % cmd) tl.write(cmd + '\r\n') if 'print' in cmd: show_msg = tl.read_until('>', 10) msgs.append(show_msg) else: msg = tl.read_until('>', 10) msgs.append(msg) tl.write('\r\n') time.sleep(1) msgs = ' '.join(msgs) print("%s" % msgs) tl.close() if 'show_msg' in locals().keys(): _searchchar(searchchar, show_msg, expect, 'show') else: _searchchar(None, msgs, expect)
def http_cli(self,host='',port=None,url='/',act='GET',expect=1,searchc=None): ''' ''' host=_unicode_to_utf(host) port=_unicode_to_utf(port) if (not port) or port == "None" or port.isspace(): port=None url=_unicode_to_utf(url) act=_unicode_to_utf(act) expect=_unicode_to_utf(expect) searchc=_unicode_to_utf(searchc) print("run keyword:%s"%(sys._getframe().f_code.co_name)) sendheader={"Accept-Encoding":"gzip, deflate, sdch",\ "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",\ #"Content-Length":"100",\ "Connection":"keep-alive",\ "Accept-Language":"zh-CN,zh;q=0.8",\ "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"} try: conn=httplib.HTTPConnection(host=host,port=port)#,timeout=10) conn.request(act,url,'',sendheader) msgs=conn.getresponse() body=msgs.read() conn.close() except: if expect == 0 or expect == '0': if searchc ==None or str(searchc) == 'None' or searchc == '': print("expect get url from host:%s fail, actually fail"%host) else : raise ExpectError("expect get url from host:%s ,connect fail"%host) else : raise ExpectError("get url from host:%s error!"%host) else: if searchc ==None or str(searchc) == 'None' or searchc == '' : if expect == 0 : raise ExpectError("expect get url from host:%s fail, actually success"%host) else : print("body:\n%s"%unicode(body,'utf8')) print("expect get url from host:%s success, actually sucess"%host) else : print("body:\n%s"%body) _searchchar(searchc,body,expect,'http')
def audit_mysql_search(self,host='',table='',expect=None,searchchar=None,username=dut_user,passwd=dut_passwd): ''' table=file_transfer,app_others,email,instant_message,online_shopping,search-enine,social_network ''' print("run keyword:%s"%(sys._getframe().f_code.co_name)) host=_unicode_to_utf(host) table=_unicode_to_utf(table) expect=_unicode_to_utf(expect) searchchar=_unicode_to_utf(searchchar) username=_unicode_to_utf(username) passwd=_unicode_to_utf(passwd) tl,msgs=self._login_diagnose(host,username,passwd) table_name=self._find_current_table(tl,table,msgs) tl.write("echo \"use syslog; select * from "+table_name+"; \" | mysql\n") msg=tl.read_until('#',self.timeout) msgs.append(msg) tl.close() msgs=' '.join(msgs) print("%s"%msgs) _searchchar('ERR',msg,0,'sql') _searchchar(searchchar,msg,expect,'sql')
def win_show_ip(self, inter='test', searchip=None, ip_type=4, times=15): print("run keyword:%s" % (sys._getframe().f_code.co_name)) msgs = [] inter = _unicode_to_utf(inter) searchip = _unicode_to_utf(searchip) ip_type = _unicode_to_utf(str(ip_type)) times = _unicode_to_utf(times) if int(ip_type) == 4: ip_type = 4 elif int(ip_type) == 6: ip_type = 6 else: raise ExpectError("ip type error") if int(ip_type) == 4: if self._working_pc_version() == 1: cmd = 'netsh interface ip show config "' + inter + '"' elif self._working_pc_version() == 2: cmd = 'netsh interface ipv4 show config "' + inter + '"' else: cmd = 'ipconfig' tm = 0 #cmd='ipconfig' for i in range(int(times)): try: print("%s" % cmd) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) p.wait() msgs.append(p.stdout.read()) msgs.append(p.stderr.read()) p.terminate() msg = '\n'.join(msgs) print("%s" % unicode(msg, global_encoding)) return _searchchar(searchip, msg, expect=1, tpye='pc') except: tm += 1 time.sleep(1) if tm == int(times): raise ExpectError("search ip %s error" % searchip) else: print("search times %d, search error" % tm)
def capture_show(self,searchc=None,expect=1): """ Function: show capture pcakets eg: | Capture Show | 192.168.0.100 | 1 | """ print("run keyword:%s"%(sys._getframe().f_code.co_name)) searchc=_unicode_to_utf(searchc) expect=int(_unicode_to_utf(expect)) print("run keyword:%s"%(sys._getframe().f_code.co_name)) print("wait for capture pcakets!") time.sleep(int(self.timeout)) pcap=[] if self.capture_t.is_alive() : self.capture_t.terminate() self.capture_t.join() for i in self.cap: pcap.append(str(i.show)) strpcap='\n'.join(pcap) print("%s"%strpcap) return _searchchar(searchc,strpcap,expect,tpye='pcap')
def capture_show(self, searchc=None, expect=1): """ Function: show capture pcakets eg: | Capture Show | 192.168.0.100 | 1 | """ print("run keyword:%s" % (sys._getframe().f_code.co_name)) searchc = _unicode_to_utf(searchc) expect = int(_unicode_to_utf(expect)) print("run keyword:%s" % (sys._getframe().f_code.co_name)) print("wait for capture pcakets!") time.sleep(int(self.timeout)) pcap = [] if self.capture_t.is_alive(): self.capture_t.terminate() self.capture_t.join() for i in self.cap: pcap.append(str(i.show)) strpcap = '\n'.join(pcap) print("%s" % strpcap) return _searchchar(searchc, strpcap, expect, tpye='pcap')
def win_show_ip(self,inter='test',searchip=None,ip_type=4,times=15): print("run keyword:%s"%(sys._getframe().f_code.co_name)) msgs=[] inter=_unicode_to_utf(inter) searchip=_unicode_to_utf(searchip) ip_type=_unicode_to_utf(str(ip_type)) times=_unicode_to_utf(times) if int(ip_type) == 4: ip_type=4 elif int(ip_type) == 6 : ip_type=6 else: raise ExpectError("ip type error") if int(ip_type) == 4: if self._working_pc_version() == 1: cmd='netsh interface ip show config "'+inter+'"' elif self._working_pc_version() == 2: cmd='netsh interface ipv4 show config "'+inter+'"' else: cmd='ipconfig' tm=0 #cmd='ipconfig' for i in range(int(times)): try : print("%s"%cmd) p=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) p.wait() msgs.append(p.stdout.read()) msgs.append(p.stderr.read()) p.terminate() msg='\n'.join(msgs) print("%s"%unicode(msg,global_encoding)) return _searchchar(searchip,msg,expect=1,tpye='pc') except : tm+=1 time.sleep(1) if tm == int(times): raise ExpectError("search ip %s error"%searchip) else: print("search times %d, search error"%tm)
def telnet_run(self,host='',commands='',expect=None,searchchar=None,timeout=None,username=dut_user,passwd=dut_passwd): """ telnet a dut and exec command,command will split by ';'. searchchar is the expect string,when expect=1,the result should contain the searchchar eg: | telnet_run | 192.168.2.132 | con t;in ge3/0;no ip add;show inter ge3/0 | 1 | search | admin | admin | when an error happend in cmd,such as '%unknown command',the program will drop an error if have show command in commands ,search result of show command;else search command have error or not """ host=_unicode_to_utf(host) commands=_unicode_to_utf(commands) expect=_unicode_to_utf(expect) searchchar=_unicode_to_utf(searchchar) timeout=_unicode_to_utf(timeout) username=_unicode_to_utf(username) passwd=_unicode_to_utf(passwd) cmds=['enable'] cmds+=commands.split(';') msgs=[] print("run keyword:%s ***************** host=%s"%(sys._getframe().f_code.co_name,host)) tl=telnetlib.Telnet(host,23,10) tl.read_until("Username:"******"Password:"******"%s"%show_msg) #msgs.append(show_msg) return _searchchar(searchchar,show_msg,expect,'show') except ExpectError as e : if i+1 == int(timeout): #msgs=' '.join(msgs) #print("%s"%msgs) raise ExpectError("search times %d error :%s"%(int(timeout),e)) else: print("search times %d error :%s"%(i,e)) time.sleep(1) else: tl.write('%s\n' % cmd) show_msg=tl.read_until('#') msgs.append(show_msg) try: print("%s"%show_msg) return _searchchar(searchchar,show_msg,expect,'show') except ExpectError as e: print("%s"%' '.join(msgs)) raise ExpectError("%s"%e) else : tl.write('%s\n' % cmd) msg=tl.read_until('#') msgs.append(msg) msgs=' '.join(msgs) print("%s"%msgs) tl.close() #_searchchar(None,msgs,expect) if 'show_msg' not in locals().keys() : _searchchar(None,msgs,expect)
def telnet_run(self, host='', commands='', expect=None, searchchar=None, timeout=None, username=dut_user, passwd=dut_passwd): """ telnet a dut and exec command,command will split by ';'. searchchar is the expect string,when expect=1,the result should contain the searchchar eg: | telnet_run | 192.168.2.132 | con t;in ge3/0;no ip add;show inter ge3/0 | 1 | search | admin | admin | when an error happend in cmd,such as '%unknown command',the program will drop an error if have show command in commands ,search result of show command;else search command have error or not """ host = _unicode_to_utf(host) commands = _unicode_to_utf(commands) expect = _unicode_to_utf(expect) searchchar = _unicode_to_utf(searchchar) timeout = _unicode_to_utf(timeout) username = _unicode_to_utf(username) passwd = _unicode_to_utf(passwd) cmds = ['enable'] cmds += commands.split(';') msgs = [] print("run keyword:%s ***************** host=%s" % (sys._getframe().f_code.co_name, host)) tl = telnetlib.Telnet(host, 23, 10) tl.read_until("Username:"******"Password:"******"%s" % show_msg) #msgs.append(show_msg) return _searchchar(searchchar, show_msg, expect, 'show') except ExpectError as e: if i + 1 == int(timeout): #msgs=' '.join(msgs) #print("%s"%msgs) raise ExpectError( "search times %d error :%s" % (int(timeout), e)) else: print("search times %d error :%s" % (i, e)) time.sleep(1) else: tl.write('%s\n' % cmd) show_msg = tl.read_until('#') msgs.append(show_msg) try: print("%s" % show_msg) return _searchchar(searchchar, show_msg, expect, 'show') except ExpectError as e: print("%s" % ' '.join(msgs)) raise ExpectError("%s" % e) else: tl.write('%s\n' % cmd) msg = tl.read_until('#') msgs.append(msg) msgs = ' '.join(msgs) print("%s" % msgs) tl.close() #_searchchar(None,msgs,expect) if 'show_msg' not in locals().keys(): _searchchar(None, msgs, expect)