python自动化管理和zabbix监控网络设备(防火墙和python自动化配置部分)

目录

前言

一、ssh配置

1.FW1

2.core-sw1

3.core-sw2

二、python自动化配置防火墙

三、验证DNAT

四、验证DNAT


前言


视频演示请访问b站主页

白帽小丑的个人空间-白帽小丑个人主页-哔哩哔哩视频

一、ssh配置

给需要自动化管理的设备配置ssh服务端用户名和密码

1.FW1

#注意不要使用本地登录的用户

aaa     
manager-user user1
password cipher Huawei@123
level 15     
service-type ssh     
quit     
quit     


user-interface vty 0 4
 authentication-mode aaa
 protocol inbound all
quit

stelnet server enable   
ssh user user1
ssh user user1 authentication-type password
ssh user user1 service-type stelnet

#注意长度为2048
rsa local-key-pair create
Y
2048

2.core-sw1

aaa     

local-user huawei password cipher huawei

local-user huawei service-type ssh telnet

local-user huawei privilege level 15

quit

stelnet server enable

user-interface vty 0 4
authentication-mode aaa
protocol inbound all

quit

rsa local-key-pair create  
Y
2048

ssh user huawei authentication-type password  

ssh user huawei service-type stelnet
quit

3.core-sw2

aaa     

local-user huawei password cipher huawei

local-user huawei service-type ssh telnet

local-user huawei privilege level 15

quit

stelnet server enable

user-interface vty 0 4
authentication-mode aaa
protocol inbound all

quit

rsa local-key-pair create  
Y
2048

ssh user huawei authentication-type password  

ssh user huawei service-type stelnet
quit

二、python自动化配置防火墙

import paramiko
import getpass
import time

ip = "1.1.1.1"

username = input("Username: ")
password = getpass.getpass("Password: ")

ssh_client = paramiko.SSHClient()

# SNAT配置
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)

print("Successfully logged in" + ip)

# 配置外网地址
command = ssh_client.invoke_shell()
command.send("system-view\n")
command.send("inter gi1/0/4\n")
command.send("ip address 132.12.12.10\n")

time.sleep(0.2)
# PNAT转化地址池

command.send("nat address-group SNAT\n")
command.send("mode pat\n")
command.send("section 0 132.12.12.10\n")
command.send("route enable\n")
time.sleep(0.2)

# PNAT源地址转化策略

command.send("nat-policy\n")
command.send("rule name pat\n")
command.send("source-zone trust\n")
command.send("destination-zone untrust\n")
command.send("source-address 172.16.0.0 16\n")
command.send("source-address 172.200.0.0 0.0.1.255\n")
command.send("source-address 172.210.2.0 0.0.1.255\n")
command.send("source-address 172.220.4.0 0.0.1.255\n")
command.send("source-address 172.230.6.0 0.0.1.255\n")
command.send("source-address 172.240.8.0 0.0.1.255\n")
command.send("source-address 172.250.10.0 0.0.1.255\n")
command.send("action source-nat address-group SNAT\n")
time.sleep(0.2)

# PNAT源地址转化策略

command.send("security-policy\n")
command.send("rule name NAT\n")
command.send("source-zone trust\n")
command.send("destination-zone untrust\n")
command.send("source-address 172.16.0.0 16\n")
command.send("source-address 172.200.0.0 0.0.1.255\n")
command.send("source-address 172.210.2.0 0.0.1.255\n")
command.send("source-address 172.220.4.0 0.0.1.255\n")
command.send("source-address 172.230.6.0 0.0.1.255\n")
command.send("source-address 172.240.8.0 0.0.1.255\n")
command.send("source-address 172.250.10.0 0.0.1.255\n")
command.send("action permit\n")
time.sleep(0.2)

# 缺省路由

command.send("ip route-static 0.0.0.0 0 132.12.12.11\n")
command.send("ospf 1\n")
command.send("default-route-advertise always\n")
command.send("q\n")
time.sleep(0.2)

# ----------------------------------------------------------------------------------------------------


# DNAT转化内网地址池
command.send("ip pool dmz-pool\n")
command.send("network 192.168.170.0 mask 255.255.255.0 \n")
command.send("gateway 192.168.170.254\n")

time.sleep(0.2)

# DNAT转化
command.send("nat server protocol udp global 132.12.12.10 80 inside 192.168.170.100 80\n")
command.send("nat server protocol tcp global 132.12.12.10 80 inside 192.168.170.100 80\n")

# 安全策略
command.send("security-policy \n")
command.send("rule name allow-http-to-dmz\n")
command.send("source-zone untrust\n")
command.send("destination-zone dmz\n")
command.send("destination-address 192.168.170.100 32\n")
command.send("action permit \n")

time.sleep(0.2)

# 允许http流量通过外网口
command.send("inter gi1/0/4\n")
command.send("service-manage http permit\n")
time.sleep(0.2)

# ----------------------------------------------------------
# 配置ospf路由,让监控区访问内部设备
command.send("inter gi1/0/2\n")
command.send("ip address 10.1.90.2 30\n")
command.send("quit\n")
command.send("ospf 1\n")
command.send("area 2\n")
command.send("network 10.1.0.0 255.255.0.0\n")
command.send("area 1\n")
command.send("network 10.1.90.0 0.0.0.3\n")
time.sleep(0.2)
time.sleep(0.2)





output = command.recv(65535)
print(output.decode('utf-8'))

ssh_client.close

运行脚本

三、验证DNAT

四、验证DNAT

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白帽小丑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值