利用Python抓取ap的登录ip去imc拿到用户信息添加到panabit白名单后记录到数据库
from pysnmp.entity.rfc3413.oneliner import cmdgen
import requests
import urllib.request
import json
import time
import pymysql
add_ipaddress_groups = []
oid_number = {
'ap1':'1.3.6.1.4.1.25506.2.75.2.2.25.1.2.20.50.49.57.56.48.49.65.49.66.87.56.49.57.49.69.48.48.48.83.66',
'ap12':'1.3.6.1.4.1.25506.2.75.2.2.25.1.2.20.50.49.57.56.48.49.65.49.66.87.56.49.57.49.69.48.48.51.55.51',
'ap11':'1.3.6.1.4.1.25506.2.75.2.2.25.1.2.20.50.49.57.56.48.49.65.48.89.71.56.49.57.50.69.48.48.49.87.56',
'ap12':'1.3.6.1.4.1.25506.2.75.2.2.25.1.2.20.50.49.57.56.48.49.65.49.66.51.56.49.57.49.69.48.48.66.55.84',
'ap13':'1.3.6.1.4.1.25506.2.75.2.2.25.1.2.20.50.49.57.56.48.49.65.49.66.51.56.49.57.49.69.48.48.66.48.89',}
#########panabit报头##########
ipqunzu_name = 'ipqu群组'
# ipqunzu_name = 'aaa' #测试IP群组
server_ip = "ip" # 主墙ip,
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36", \
'Content-type': 'text/html', }
url = "http://{}:8090/cgi-bin/sendDoLogin".format(server_ip)
rq = requests.post(url=url, data={"chkport": "密钥"},headers=headers)
ret_val_data = json.loads(rq.content.decode("utf-8"))
ret_val = ret_val_data.get('data').get('token')
url3 = "http://{}:8090/cgi-bin/sendIPTableList/?token={}".format(server_ip, ret_val)
req = requests.post(url3, headers=headers).content.decode("gbk")
ret_val1 = eval(req).get("data")
#imc报头
authhandler = urllib.request.HTTPDigestAuthHandler()
authhandler.add_password("iMC RESTful Web Services", "imc-ip:8080", 'user', 'passwd')
opener = urllib.request.build_opener(authhandler)
opener = urllib.request.install_opener(opener)
def panabit(chaxu_ipadd):
for add_canshu in ret_val1:
if add_canshu.get("sName") == ipqunzu_name:
add_canshu['ip'] = chaxu_ipadd
break
url2 = "http://{}:8090/cgi-bin/sendIPTableAddIP/?token={}".format(server_ip, ret_val)
req = requests.post(url2, data=add_canshu, headers=headers).content.decode("utf8")
if eval(req).get('msg') == "success.":
return ['panabit_success']
else:
return ["panabit_fail"]
def imc(chaxu_ipadd):
user_api = "http://icm-ip:8080/imcrs/uam/online/detail?framedIp={}&showDeployUserGroup=false".format(chaxu_ipadd)
try:
pagehandle = urllib.request.Request(user_api)
pagehandle.add_header('Accept', 'application/json')
result = urllib.request.urlopen(pagehandle)
user_info = json.loads(result.read())
except Exception as e:
return ["IP地址是guest",' ',' ']
else:
return [ user_info.get('userName'),user_info.get('fullName'),\
user_info.get('serviceTemplateName')]
# 写入数据库
conn = pymysql.connect(
host="数据库ip",
port=8080,
user="user",
passwd="password",
db="库",
charset="utf8"
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = 'insert into 表名(ip,ap_name,username,number,department,Presult,time) value(%s,%s,%s,%s,%s,%s,%s);'
#########程序运行main
for k,v in oid_number.items():
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorindex, varBindTable = cmdGen.bulkCmd(
cmdgen.CommunityData('团体字'),
cmdgen.UdpTransportTarget(('AC_IP',161)),
0,25,
v,)
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
if val.prettyPrint().startswith("10.32.xx"):continue ##排除vip网段的ip
if val.prettyPrint().startswith("10.32.xx"):continue ##排除10.32.xx开通的有线网段的ip
add_ipaddress_groups.append({k:val.prettyPrint()})
print(add_ipaddress_groups)
for ss in add_ipaddress_groups:
for APname,chaxu_ipadd in ss.items():
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
imc_jg = imc(chaxu_ipadd)
pana_jg=panabit(chaxu_ipadd)
print('存库信息:',chaxu_ipadd,APname,imc_jg[0],imc_jg[1],imc_jg[2],pana_jg,current_time)
rows = cursor.execute(sql, (chaxu_ipadd,APname,imc_jg[0],imc_jg[1],imc_jg[2],pana_jg,current_time))
conn.commit()