admin管理员组

文章数量:1565370

from netmiko.ssh_dispatcher import ConnectHandler

import pymysql

import time

device = {

"device_type": "huawei",

"ip": "ip",

"username": "backup",

"password": "密码",

}

with ConnectHandler(**device) as ssh_conn:

output = ssh_conn.send_command_timing( # command_string可以较好的回显数据

command_string="""

system-view

firewall realtime-statistics enable

diagnose

display firewall online ip traffic top10

display firewall online ip session top10

quit

undo firewall realtime-statistics enable

quit

quit

"""

)

conn = pymysql.connect(host="10.32.236.8",

port=8080,

user="root",

passwd="xxx",

db="firewall",

charset="utf8", )

cursor = conn.cursor(pymysql.cursors.DictCursor)

sql = 'insert into topn(paihang,zhi,time_now) value(%s,%s,%s);'

time_now=time.strftime('%Y-%m-%d %H:%M:%S')

a =0

for s in output.splitlines():

a += 1

if 15 < a < 27 or 34 < a < 46:

try:

num = s.split(' ')

shuzi=num[1] + ':' + num[3]

cursor.execute(sql, (shuzi,num[-1],time_now))

connmit()

except Exception as e :

if 16 < a < 27 or 35 < a < 46:

num = s.split(' ')

shuzi=num[1] + ':' + num[3]

cursor.execute(sql, (shuzi, num[-1], time_now))

connmit()

—————————————第二版---------------------------------------------------

from netmiko.ssh_dispatcher import ConnectHandler

import pymysql

import time

H3C = {

"device_type": "huawei",

"ip": "ip",

"username": "账号",

"password": "密码",

}

conn = pymysql.connect(host="",

port=8080,

user="root",

passwd="密码",

db="库名",

charset="utf8", )

cursor = conn.cursor(pymysql.cursors.DictCursor)

sql = 'insert into 表名(direction,paihang,time_now) value(%s,%s,%s);'

net_connect = ConnectHandler(**H3C)

net_connect.send_command(command_string='display firewall topn source-ip traffic',expect_string ='')

net_connect.send_command(command_string=r'Y',expect_string = '>',cmd_verify = False)

time.sleep(12)

output = net_connect.send_command(command_string=r'',expect_string = '',cmd_verify = False)

time.sleep(2)

time_now = time.strftime('%Y-%m-%d %H:%M:%S')

cursor.execute(sql, ('source_ip',output,time_now))

time.sleep(1)

# print(output)

net_connect.send_command(command_string='display firewall topn destination-ip traffic',expect_string ='')

time.sleep(14)

outpute = net_connect.send_command(command_string=r'',expect_string = '>',cmd_verify = False)

time.sleep(2)

net_connect.send_command(command_string=r'quit',expect_string = '',cmd_verify = False)

cursor.execute(sql, ('destination_ip',outpute,time_now))

# print(outpute)

connmit()

conn.close()

本文标签: 华为防火墙流量数据TOP