def upgrade(self): try: self.post_file(self.post_url, self.firmware, 3) except requests.RequestException: self.print_with_lock(self.addr + ': send firmware failed') return else: self.print_with_lock(self.addr + ': send firmware success, then sleep 1 second') time.sleep(1) # send upgrade instruction 3 times to ensure send successfully # TODO: how to check weather upgrade successfully for x in xrange(3): try: self.headers['Referer'] = self.post_url HttpHelper.connect_auth_with_headers(None, self.upgrade_url, 2, (self.username, self.password), self.headers) except ErrorTimeout: pass self.print_with_lock(self.addr + ': send upgrade instruction')
def connect_type_rec(self): jump_url = self.base_url + '/BAS_basic.htm' r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) type_re = 'select_basic="(.)"' type_ret = re.compile(type_re, re.I).search(r.content) if type_ret == '1': return 'dyna' elif type_ret == '0': return 'pppoe' else: return 'static'
def connect_type_rec(self): jump_url = self.base_url + '/BAS_basic.htm' r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) type_re = 'select_basic="(.)"' type_ret = re.compile(type_re, re.I).search(r.content) if type_ret == '1': return 'dyna' elif type_ret == '0': return 'pppoe' else: return 'static'
def get_now_info(self, internet_type): bas_ref = self.base_url + '/BAS_basic.htm' self.headers[b'Referer'] = bas_ref dyna_bas_url = self.base_url + '/BAS_ether.htm' pppoe_bas_url = self.base_url + '' static_bas_url = self.base_url + '' if internet_type == 'static': jump_url = static_bas_url elif internet_type == pppoe_bas_url: jump_url = '' else: jump_url = dyna_bas_url r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) return r.content
def get_now_info(self, internet_type): bas_ref = self.base_url + '/BAS_basic.htm' self.headers[b'Referer'] = bas_ref dyna_bas_url = self.base_url + '/BAS_ether.htm' pppoe_bas_url = self.base_url + '' static_bas_url = self.base_url + '' if internet_type == 'static': jump_url = static_bas_url elif internet_type == pppoe_bas_url: jump_url = '' else: jump_url = dyna_bas_url r = HttpHelper.connect_auth_with_headers(None, jump_url, 3, (self.username, self.password), self.headers) return r.content
def connect_auth_with_headers(self, url, times): r = HttpHelper.connect_auth_with_headers(self.session, url, times, (self.username, self.password), self.headers) return r
def connect_auth_with_headers(self, url, times): r = HttpHelper.connect_auth_with_headers(self.session, url, times, (self.username, self.password), self.headers) return r
def dns_set(self, dns): url = 'http://' + self.addr + ':' + str( self.port) + self.wan_type_search self.headers['Referer'] = 'http://' + self.addr + ':' + str(self.port) try: r = HttpHelper.connect_auth_with_headers( None, url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': fail, connect timeout') return ref_re = 'location.href="(.+?)"' ref_re_index = 1 ref_pattern = re.compile(ref_re, re.I) match = ref_pattern.search(r.content) if match: wan_url = 'http://' + self.addr + ':' + str( self.port) + match.group(ref_re_index) else: self.print_with_lock(self.addr + ': fail, can not find the wan_url') return if wan_url.find('WanDynamic') > 0: payload = self.__dyna_payload(dns[0], dns[1]) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Wan Dynamic') elif wan_url.find('PPPoE') > 0: payload = self.__ppp_payload(dns[0], dns[1]) dns_url = wan_url.replace('PPPoECfgRpm', 'PPPoECfgAdvRpm') + payload if self.debug: self.print_with_lock(self.addr + ': PPPoE') elif wan_url.find('WanStatic') > 0: try: r = HttpHelper.connect_auth_with_headers( None, 3, (self.username, self.password), self.headers) static_ip_info_re = 'var staticIpInf = new Array.+"(.+?)".{2}"(.+?)".{2}"(.+?)".{2}1500,' static_ip_info_pattern = re.compile(static_ip_info_re, re.I | re.S) static_ip_info_match = static_ip_info_pattern.search(r.content) if not static_ip_info_match: raise ErrorTimeout else: static_ip = static_ip_info_match.group(1) static_mask = static_ip_info_match.group(2) static_gateway = static_ip_info_match.group(3) except ErrorTimeout: self.print_with_lock( self.addr + ': timeout, static ip but can not load the info page') return payload = self.__static_payload(dns[0], dns[1], static_ip, static_mask, static_gateway) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Static IP') else: self.printLock( self.addr + ': fail, can not find the dns change method in this router') return if self.debug: self.print_with_lock(dns_url) try: r = HttpHelper.connect_auth_with_headers( None, dns_url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': maybe fail, no response') else: if r.content.find(dns[0]) > 0: self.print_with_lock(self.addr + ': success') else: self.print_with_lock(self.addr + ': fail, change dns fail')
def dns_set(self, dns): url = 'http://' + self.addr + ':' + str(self.port) + self.wan_type_search self.headers['Referer'] = 'http://' + self.addr + ':' + str(self.port) try: r = HttpHelper.connect_auth_with_headers(None, url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': fail, connect timeout') return ref_re = 'location.href="(.+?)"' ref_re_index = 1 ref_pattern = re.compile(ref_re, re.I) match = ref_pattern.search(r.content) if match: wan_url = 'http://' + self.addr + ':' + str(self.port) + match.group(ref_re_index) else: self.print_with_lock(self.addr + ': fail, can not find the wan_url') return if wan_url.find('WanDynamic') > 0: payload = self.__dyna_payload(dns[0], dns[1]) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Wan Dynamic') elif wan_url.find('PPPoE') > 0: payload = self.__ppp_payload(dns[0], dns[1]) dns_url = wan_url.replace('PPPoECfgRpm', 'PPPoECfgAdvRpm') + payload if self.debug: self.print_with_lock(self.addr + ': PPPoE') elif wan_url.find('WanStatic') > 0: try: r = HttpHelper.connect_auth_with_headers(None, 3, (self.username, self.password), self.headers) static_ip_info_re = 'var staticIpInf = new Array.+"(.+?)".{2}"(.+?)".{2}"(.+?)".{2}1500,' static_ip_info_pattern = re.compile(static_ip_info_re, re.I | re.S) static_ip_info_match = static_ip_info_pattern.search(r.content) if not static_ip_info_match: raise ErrorTimeout else: static_ip = static_ip_info_match.group(1) static_mask = static_ip_info_match.group(2) static_gateway = static_ip_info_match.group(3) except ErrorTimeout: self.print_with_lock(self.addr + ': timeout, static ip but can not load the info page') return payload = self.__static_payload(dns[0], dns[1], static_ip, static_mask, static_gateway) dns_url = wan_url + payload if self.debug: self.print_with_lock(self.addr + ': Static IP') else: self.printLock(self.addr + ': fail, can not find the dns change method in this router') return if self.debug: self.print_with_lock(dns_url) try: r = HttpHelper.connect_auth_with_headers(None, dns_url, 3, (self.username, self.password), self.headers) except ErrorTimeout: self.print_with_lock(self.addr + ': maybe fail, no response') else: if r.content.find(dns[0]) > 0: self.print_with_lock(self.addr + ': success') else: self.print_with_lock(self.addr + ': fail, change dns fail')