def reboot_router(): try: #在路由器中输入reboot ssh.ssh_cmd2('reboot') time.sleep(60) #捕捉异常并打印异常信息 except Exception,e: print u"reboot重启路由失败,原因如下:\n%s"%e
def step_100msh0286(self): #修改请求管理接口的时间间隔为1分钟 content = ['0 2 * * * /bin/rand_upgrade_msh\n', '0 4 * * * /sbin/rand_reboot\n', '0 6 * * * /etc/init.d/sysntpd restart\n', '*/1 * * * * /sbin/get_manage\n', '0 1 * * * /sbin/cwifi_pwd\n'] modify_remote_file('/etc/crontabs/','root',content) #重启路由定时机制 ssh.ssh_cmd2('/etc/init.d/cron restart') time.sleep(10) #上传tcpdump到路由器 #上个用例已经上传了,这里就不再上传了 d = data.ssh_user() #tcpdump_control.scp_to_remote('./data/BHU_tcpdump/tcpdump',d[0],d[1],d[2],'/usr/sbin/') #tcpdump_control.scp_to_remote('./data/BHU_tcpdump/libpcap.so.1.3',d[0],d[1],d[2],'/usr/lib/') #ssh登录路由输入tcpdump抓包 wanlog = capture_wan_packet() #将抓到的封包传输回本地pc tcpdump_control.scp_to_local(d[0],d[1],d[2],wanlog,'./data/') #打开本地下载的文件,读取文件内容 f = open('./data/wanlog') log = f.read() f.close() '''#获取路由网关ID gw = general_control.get_gatewayId(self) #路由mac r_mac = ssh.ssh_cmd2("ifconfig eth0 | grep HWaddr | awk '{print$5}'") R_MAC = r_mac.upper() #路由版本 r_version = ssh.ssh_cmd2('cat /etc/version/version') R_VERSION = r_version.upper() #路由无线mac wlan_mac = ssh.ssh_cmd2("ifconfig wlan0 | grep HWaddr | awk '{print$5}'") WLAN_MAC = wlan_mac.upper()''' #正确的patch信息字符如下 patch_str1 = 'GET /manage/patch?gw_id=' #patch_str2 = '%s&route_mac=%s&route_version=%s&patch_md5='%(gw,R_MAC,R_VERSION) #正确的manage信息字符如下 manage_str1 = 'GET /manage/manage?gw_id=' #manage_str2 = '%s&route_mac=%s&route_version=%s&manage_md5='%(gw,R_MAC,R_VERSION) if (patch_str1 and manage_str1) in log: #patch和manage信息在log信息中,说明有管理接口请求,结果赋值1 result = 1 else: #patch和manage信息不在log信息中,说明没有管理接口请求,结果赋值0 result = 0 #结果返回给函数 return result
def ssh_get_weblist(): try: cmd = "cat /etc/config/wifidog" result = ssh.ssh_cmd2(cmd) return result except Exception,e: print u"ssh获取wifidog配置信息失败,原因如下:%s"%e
def iptables_nat(self,ip): iptable = 'iptables -t nat -L |grep %s'%ip #查看iptables的nat表,并过滤IP地址 list = ssh.ssh_cmd2(iptable) #list = ssh.ssh_cmd(host,usr,pwd,iptable) #单独调试时使用 dmz_host1 = '/* DMA */ to:%s'%ip #防火墙WAN_rule策略查找 dmz_host2 = '/* DMA (reflection) */ to:%s'%ip ##防火墙LAN_rule策略查找 lan_rule = False wan_rule = False #在iptables查询结果中搜索需要的值,如果搜到就将wan_rule置为True,否则抛异常 if dmz_host1 in list: wan_rule = True else: raise ValueError(u'未在iptables wan_rule策略中查找到DMZ主机') #在iptables查询结果中搜索需要的值,如果搜到就将lan_rule置为True,否则抛异常 if dmz_host2 in list: lan_rule = True else: raise ValueError(u'未在iptables lan_rule策略中查找到DMZ主机') #当lan_rule和wan_rule同时满足条件时,打印策略成功,否则抛异常 if lan_rule and wan_rule == True: print u'DMZ策略设置成功' else: raise ValueError(u'DMZ策略设置失败')
def router_access_internet(self): try: result = ssh.ssh_cmd2('ping www.baidu.com -c 3') return "0% packet loss" in result #捕捉异常并打印异常信息 except Exception,e: print u"ssh登录路由取值失败,原因如下:\n%s"%e
def router_wan_inet(): try: ifocnfig = ssh.ssh_cmd2('ifconfig eth1') return ifocnfig #捕捉异常并打印异常信息 except Exception,e: print u"查看路由器wan口的网络信息失败,原因如下:\n%s"%e
def step_100msh0078(self): try: #登录路由取dns值 dns = ssh.ssh_cmd2('cat /tmp/resolv.conf.auto') return dns #捕捉异常并打印异常信息 except Exception,e: print u"登录路由取dns值的过程失败,原因如下:\n%s"%e
def step_100msh0080(self): try: #设置主机名 change_hostname(self,'100msh.com') #登录路由取hostname值 hostname = ssh.ssh_cmd2('cat /etc/config/network | grep hostname') return hostname #捕捉异常并打印异常信息 except Exception,e: print u"设置主机名的过程失败,原因如下:\n%s"%e
def router_access_internet(): try: ssh_user = data.ssh_user() result = ssh.ssh_cmd2('ping www.baidu.com -c 3') if "0% packet loss" in result: return True else: return False #捕捉异常并打印异常信息 except Exception,e: print u"ssh登录路由取值失败,原因如下:\n%s"%e
def ssh_wifidog(): try: i =0 while(i<5): result = ssh.ssh_cmd2("ps") if "wifidog" in result: return True time.sleep(20) i+=1 return False except Exception,e: print u"从ssh获取wifidog进程信息失败,原因如下:%s"%e
def iptables_dmz_disable(self,ip): iptable = 'iptables -t nat -L |grep %s'%ip #查看iptables的nat表,并过滤IP地址 list = ssh.ssh_cmd2(iptable) #list = ssh.ssh_cmd(host,usr,pwd,iptable) #单独调试时使用 dmz_host1 = '/* DMA */ to:%s'%ip #防火墙WAN_rule策略查找 dmz_host2 = '/* DMA (reflection) */ to:%s'%ip ##防火墙LAN_rule策略查找 lan_rule = False wan_rule = False #在iptables查询结果中搜索需要的值,如果搜到就将wan_rule置为True,否则抛异常 if dmz_host1 in list: raise ValueError(u'DMZ禁用后,仍在iptables wan_rule策略中查找到DMZ主机') else: print u'DMZ禁用后,iptables wan_rule策略中DMZ主机已清除' #在iptables查询结果中搜索需要的值,如果搜到就将lan_rule置为True,否则抛异常 if dmz_host2 in list: raise ValueError(u'DMZ禁用后,仍在iptables lan_rule策略中查找到DMZ主机') else: print u'DMZ禁用后,iptables lan_rule策略中DMZ主机已清除' print u'DMZ策略禁用成功'
def reboot(): ssh.ssh_cmd2("reboot") time.sleep(60)
def ssh_get_trusted(): try: truse = ssh.ssh_cmd2("iptables -t mangle -L WiFiDog_br-lan_Trusted") return truse except Exception,e: print u"从ssh获取iptables表中终端百白名单失败。原因如下:%s"%e
def ssh_reboot(): data = ssh.ssh_cmd2("reboot")
def ssh_get_outgoing(): try: outgoing = ssh.ssh_cmd2("iptables -t mangle -L WiFiDog_br-lan_Outgoing") return outgoing except Exception,e: print u"从ssh获取iptables表中WiFiDog_br-lan_Outgoing信息失败。原因如下:%s"%e
def ssh_privoxy(): result = ssh.ssh_cmd2("ps") return "privoxy" in result