查看华为防火墙会话和流量top数据

13 篇文章 0 订阅

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))

conn.commit()

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))

conn.commit()

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

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)

conn.commit()

conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值