h3c(or huawei)_firewall_dnat(python)

import requests
import xmltodict
import pymysql
#总部华为防火墙
firewall_ip_dict ={
    'xx出口防火墙':'10.84.xx',
    'xxx防火墙': '10.84.xx',
    'xxxxx防火墙':'10.84.xx' }

conn = pymysql.connect(
    host="10.32.xx",
    port=8080,
    user="user",
    passwd="xx",
    db="firewall_natserver",
    charset="utf8"
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = 'insert into firewall_nat_info(firewall_name,PN_ip,PN_port,agreement,IN_ip,IN_port,des) value(%s,%s,%s,%s,%s,%s,%s);'

for firewall_name,firewall_ip in firewall_ip_dict.items():
    url = 'http://{0}:9000/restconf/data/huawei-nat-server:nat-server'.format(firewall_ip)
    req = requests.get(url,headers={'Authorization': 'Basic xxxxxxxxxxxxxxx'}).text
    xml_doc = xmltodict.parse(req)
    dnat_data =xml_doc['reply']['data']['nat-server']['server-mapping']

    for nat in dnat_data:
        if nat.get('protocol') == '6':#判断是否是tcp/udp
            agreement = "TCP"
        elif nat.get('protocol') == '17':
            agreement = "UDP"
        else:
            agreement = "ALL"

        if  isinstance(nat.get('global-port'),dict):#判断是否是端口信息
            PN_port =nat.get('global-port').get('start-port')
        else:
            PN_port = 'ALL_Port'
        if  isinstance(nat.get('inside-port'),dict):
            IN_port = nat.get('inside-port').get('start-port')
        else:
            IN_port = 'ALL_Port'
    
        print('防火墙名称:', firewall_name,
              '公网ip:', nat.get('global').get('start-ip'),
              '公网端口:', PN_port,
              '协议:', agreement,
              '内网ip:', nat.get('inside').get('start-ip'),
              '内网端口:', IN_port,
              '描述:', nat.get('name'))
        rows = cursor.execute(sql, (firewall_name,nat.get('global').get('start-ip'),PN_port,agreement,
                                    nat.get('inside').get('start-ip'),IN_port,nat.get('name')))
        conn.commit()


#H3C防火墙
url = "http://10.123.xxx/api/v1/tokens"
req = requests.post( url, headers={"Authorization": "Basic xxxxxxxxxxxxxx"})
token_id = req.json().get('token-id')

url2 = "http://10.123.xx.x/api/v1/NAT/ServerOnInterfaces"
req = requests.get(url2,headers={"X-Auth-Token": token_id})
dnat_data = req.json().get('ServerOnInterfaces')

for dnat in dnat_data:
    if  dnat.get('Disable'):continue  #禁用策略不显示
    if dnat.get('ProtocolType') == 6: #判断tcp\udp协议
        agreement ='TCP'
    elif dnat.get('ProtocolType') == 17:
        agreement ='UDP'
    else:
        agreement ='ALL'

    if dnat.get('GlobalInfo').get('GlobalEndPortNumber'): #判断公网侧是否有连续端口
        PN_port = str(dnat.get('GlobalInfo').get('GlobalStartPortNumber')) +'-'+str(dnat.get('GlobalInfo').get('GlobalEndPortNumber'))
    else:
        PN_port = dnat.get('GlobalInfo').get('GlobalStartPortNumber')
    if dnat.get('LocalInfo').get('LocalEndPortNumber'):#判断内网侧是否有连续端口
        IN_port = str(dnat.get('LocalInfo').get('LocalStartPortNumber')) +'-'+str(dnat.get('LocalInfo').get('LocalEndPortNumber'))
    else:
        IN_port = dnat.get('LocalInfo').get('LocalStartPortNumber')

    print('防火墙名称:', 'H3c边界防火墙',
          '公网ip:',dnat.get('GlobalInfo').get('GlobalStartIpv4Address'),
          '公网端口:',PN_port,
          '协议:',agreement,
          '内网ip:',dnat.get('LocalInfo').get('LocalStartIpv4Address'),
          '内网端口:',IN_port,
          '描述:',dnat.get('Description'))
    rows = cursor.execute(sql, ('H3c边界防火墙', dnat.get('GlobalInfo').get('GlobalStartIpv4Address'), PN_port,
                                agreement, dnat.get('LocalInfo').get('LocalStartIpv4Address'),IN_port, dnat.get('Description')))
    conn.commit()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要批量备份华为、H3C交换机配置,可以使用 Python 的 Paramiko 模块来实现。以下是实现步骤: 1. 安装 Paramiko 模块 可以使用 pip 安装 Paramiko 模块:`pip install paramiko` 2. 编写 Python 脚本 ```python import paramiko # 定义交换机信息 switches = [ {'hostname': '192.168.1.1', 'username': 'admin', 'password': '123456', 'brand': 'huawei'}, {'hostname': '192.168.1.2', 'username': 'admin', 'password': '123456', 'brand': 'h3c'} ] # 遍历交换机列表 for switch in switches: # 连接交换机 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(switch['hostname'], username=switch['username'], password=switch['password']) # 根据品牌执行备份命令 if switch['brand'] == 'huawei': # 华为交换机备份命令 command = 'save configuration to tftp 192.168.1.3 VR-Mgmt-all-backup.cfg' elif switch['brand'] == 'h3c': # H3C交换机备份命令 command = 'backup startup-configuration to 192.168.1.3' # 执行备份命令 stdin, stdout, stderr = ssh.exec_command(command) # 输出备份结果 print(f"{switch['hostname']} backup {'successful' if not stderr.read() else 'failed'}") # 关闭连接 ssh.close() ``` 以上代码中,我们定义了一个 `switches` 列表,其中包含了多个交换机的信息,包括主机名、用户名、密码和品牌。然后遍历列表,根据品牌执行相应的备份命令,并输出备份结果。最后关闭连接。 在华为交换机备份命令中,我们使用了 `save configuration to tftp` 命令将配置备份到 TFTP 服务器上,需要提前在 TFTP 服务器上安装和配置 TFTP 服务。在 H3C交换机备份命令中,我们使用了 `backup startup-configuration to` 命令将配置备份到指定地址上。 注意:在执行 SSH 连接时,需要确认 SSH 服务已经启动,并且需要在防火墙中允许 SSH 服务通过。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值