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数据 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1726583131a1076344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论