def set(): if request.method == 'GET': resp_date = {} info = None resp_date['info'] = info return ops_render('mon/set.html', resp_date) resp = {'code': 200, 'msg': '设置成功', 'data': {}} req = request.values ip = req['ipaddr'] if 'ipaddr' in req else '' if ip is None: resp['code'] = -1 resp['msg'] = '交换机输入错误' return jsonify(resp) for item in json.loads(ip): has_in = Monitor.query.filter(Monitor.ipaddr == item).first() if has_in: resp['code'] = -1 resp['msg'] = 'IP地址:%s 已存在,请更改' % item return jsonify(resp) db.session.execute(Monitor.__table__.insert(), [{ 'ipaddr': item, 'create_time': getCurrentDate(), 'update_time': getCurrentDate() }]) db.session.commit() return jsonify(resp)
def OpsFlByOpenId(openId): fl_info = None fl_info = Follower.query.filter(Follower.OpenId == openId).first() if fl_info: return fl_info url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN'.format( WeChatService.getAccessToken(), openId) res = requests.get(url) fl = res.json() follower = Follower() follower.CreateTime = getCurrentDate() follower.NickName = fl['nickname'] follower.Sex = fl['sex'] follower.HeadImgUrl = fl['headimgurl'] follower.OpenId = fl['openid'] follower.Language = fl['language'] follower.Subscribe_time = fl['subscribe_time'] follower.UpdateTime = getCurrentDate() db.session.add(follower) db.session.commit() return follower
def nmap(): with app.app_context(): info = Monitor.query.all() if info is None: exit(1) ip = '' for item in info: ip = ip + item.ipaddr + ' ' nmap = nmap3.NmapHostDiscovery() results = nmap.nmap_no_portscan(ip) db.session.execute(Monitor.__table__.update(), [{'isok': '0'}]) db.session.commit() if results is None: exit(1) for i in range(0, len(results['hosts'])): ip = results['hosts'][i]['addr'] status = 1 if results['hosts'][i]['state'] == 'up' else '0' mon = Monitor.query.filter_by(ipaddr=ip).first() if mon: mon.update_time = getCurrentDate() mon.isok = status db.session.add(mon) db.session.execute(MonLog.__table__.insert(), [{ 'mon_id': mon.id, 'update_time': getCurrentDate() }]) db.session.commit()
def run(self, ret_params): url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid='.format( WeChatService.getAccessToken()) res = requests.get(url) openIds = res.json()['data']['openid'] for openId in openIds: url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN'.format( WeChatService.getAccessToken(), openId) res = requests.get(url) fl = res.json() fl_into = Follower.query.filter(Follower.OpenId == openId).first() if fl_into: follower = fl_into else: follower = Follower() follower.CreateTime = getCurrentDate() follower.NickName = fl['nickname'] follower.Sex = fl['sex'] follower.HeadImgUrl = fl['headimgurl'] follower.OpenId = fl['openid'] follower.Language = fl['language'] follower.Subscribe_time = fl['subscribe_time'] follower.UpdateTime = getCurrentDate() db.session.add(follower) db.session.commit()
def __status(self, id, status): info = Sw.query.filter_by(id=id).first() info.status = status info.update_time = getCurrentDate() db.session.add(info) db.session.commit() return True
def wechat_msg(): signature = request.values['signature'] timestamp = request.values['timestamp'] nonce = request.values['nonce'] tmp = [token, timestamp, nonce] tmp.sort() tmp = ''.join(tmp) tmp = hashlib.sha1(tmp.encode('utf-8')).hexdigest() if tmp == signature: if request.method == "GET": echostr = request.values['echostr'] return echostr xml = request.data code = xml_to_dict(xml) result = 'success' # 判断openid是否存在 fl_info = FollowerSevice.OpsFlByOpenId(code['FromUserName']) if fl_info: if code['MsgType'] == 'text': content = code['Content'] if code['Content'] == '签到': SignInService.opsSign(fl_info) elif code['Content'] == '万圣节': mediaId = '56BTVr0HEMnwI9koUHtaq8kA452NcCVVdhyhgE0jJkA' FollowerSevice.send_img(mediaId, fl_info.OpenId) elif code['MsgType'] == 'image': content = code['PicUrl'] else: if code['MsgType'] == 'event': if code['Event'] == 'subscribe': FollowerSevice.send_msg( '终于等到你 还好我没放弃\r\n\r\n美货美铺,八年专业海淘经验\r\n您身边值得信任的海淘专家!\r\n晨大人 微信:shijimonian', fl_info.OpenId) cl = ConversationLog() cl.CreateTime = getCurrentDate() cl.ToUserName = code['ToUserName'] cl.FromUserName = code['FromUserName'] cl.Content = content db.session.add(cl) db.session.commit() # tm = int(time.time()) # result = { # 'ToUserName': code['FromUserName'], # 'FromUserName': code['ToUserName'], # 'CreateTime': tm, # 'MsgType': 'text', # 'Content': str # } # result = dict_to_xml(result) return result
def test(): # f = open('e:\\git\\order\\1.txt', 'r') # HOST='192.168.0.1' # ss=getFormat(reslt=f.readlines(),ip=HOST) # f.close() resp = {'code': 200, 'msg': '设置成功', 'data': {}} sw_info = Sw.query.all() for item in sw_info: comm = app.config['BASE_COMM'] print('---------------') print(item.IsCore) print(item.IsCore == 0) if item.IsCore == 0: comm.extend(app.config['MAC_COMM']) else: comm.extend(app.config['ARP_COMM']) print(comm) list = telnetdo(HOST=item.IpAdd, USER=item.User, PASS=item.Passwd, COMMAND=comm) info = Sw.query.filter_by(Id=item.Id).first() if list.find('GE') < 0 or list.find('Eth') < 0: info.status = 1 resp['data'] = {item.IpAdd: False} else: isok = getFormat(reslt=list, ip=item.IpAdd, iscore=item.IsCore) if isok: resp['data'] = {item.IpAdd: True} info.status = '0' else: resp['data'] = {item.IpAdd: False} info.status = 1 info.UpdateTime = getCurrentDate() db.session.add(info) db.session.commit() return jsonify(resp)
def set(): default_pwd = "******" if request.method == 'GET': resp_date = {} req = request.values info = None swid = int(req.get('id', 0)) if swid: info = Sw.query.filter_by(id=swid).first() resp_date['info'] = info return ops_render('switch/set.html', resp_date) resp = {'code': 200, 'msg': '设置成功', 'data': {}} req = request.values id = req['id'] if 'id' in req or 'id' == None else 0 swip = req['ipaddr'] if 'ipaddr' in req else '' login_name = req['login_name'] if 'login_name' in req else '' login_pwd = req['login_pwd'] if 'login_pwd' in req else '' iscore = req['iscore'] if 'iscore' in req else 0 note = req['note'] if 'note' in req else '' if swip is None: resp['code'] = -1 resp['msg'] = '交换机输入错误' return jsonify(resp) if login_name is None or len(login_name) < 1: resp['code'] = -1 resp['msg'] = '交换机登陆名输入错误' return jsonify(resp) if login_pwd is None or len(login_pwd) < 2: resp['code'] = -1 resp['msg'] = '交换机登陆密码输入格式错误' return jsonify(resp) if id != '': sw_info = Sw.query.filter_by(id=id).first() if sw_info: model_sw = sw_info else: model_sw = Sw() model_sw.iscore = iscore model_sw.user = login_name model_sw.note = note model_sw.create_time = getCurrentDate() if login_pwd != default_pwd: model_sw.passwd = login_pwd db.session.add(model_sw) db.session.commit() return jsonify(resp) for item in json.loads(swip): has_in = Sw.query.filter_by(ipaddr=item).first() if has_in: resp['code'] = -1 resp['msg'] = '交换机%s已存在,请更改' % item return jsonify(resp) # db.session.execute( # Sw.__table__.insert(), # [{'Passwd': login_pwd, 'User': login_name, 'Ipaddr': item, 'iscore': iscore, # 'create_time': getCurrentDate()}] # ) info = Sw() info.iscore = iscore info.user = login_name info.note = note info.passwd = login_pwd info.create_time = getCurrentDate() info.ipaddr = item db.session.add(info) db.session.commit() return jsonify(resp)
def __getformat(self, reslt, ip, iscore): if len(reslt) < 10: app.logger.info('-------1') return False a = {} b = [] # 读取每行,格式化形成一个mac:端口的列表和端口表 xlist = reslt.split('\r\n') for item in xlist: if str(item).find('GE') != -1 or str(item).find('Eth') != -1: list = item.split(' ') while '' in list: list.remove('') print(iscore) if iscore == 1: arp_info = ARP.query.filter_by(ipaddr=list[0]).first() if arp_info: arp_info.macadd = list[1] arp_info.update_time = getCurrentDate() db.session.add(arp_info) db.session.commit() else: db.session.execute(ARP.__table__.insert(), [{ 'ipaddr': list[0], 'macadd': list[1], 'swip': ip, 'update_time': getCurrentDate() }]) db.session.commit() else: b.append(list[2]) a[list[0]] = list[2] if iscore == 1: return True if len(b) < 1: return False # ss=getFormat(reslt=f,ip=HOST) c = collections.Counter(b) nums_dict = dict(collections.Counter(b)) # 保留只有1个MAC的键值 li = [] for k, v in nums_dict.items(): if v == 1: li.append(k) # 从a中选出只有一个的mac和端口对应 for key in a: if str(a[key]).isalnum(): continue if a[key] in li: mac_info = MAC.query.filter_by(macadd=key).first() if mac_info: mac_info.port = a[key] mac_info.update_time = getCurrentDate() db.session.add(mac_info) db.session.commit() else: db.session.execute(MAC.__table__.insert(), [{ 'port': a[key], 'macadd': key, 'swip': ip, 'update_time': getCurrentDate() }]) db.session.commit() return True