saltstack的探索-执行脚本和命令来更新防火墙配置

简介:

saltstack的探索-执行脚本和命令来更新防火墙配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
需求:zabbix-agent服务需调整防火墙,增加端口10050
 
[root@master salt] # cat firewall/init.sls 
/home/ops/bin/firewall_add_dport .sh:
   file .managed:
     source : salt: //firewall/bin/firewall_add_dport .sh
     - mode: 755
 
iptables-add-dport:
   cmd.run:
     - require:
       file /home/ops/bin/firewall_add_dport .sh
     - name:  /bin/bash  /home/ops/bin/firewall_add_dport .sh
 
[root@master salt] # cat firewall/bin/firewall_add_dport.sh 
#!/bin/bash
# 2015/4/10
 
s_port=10050
 
echo  "[-] add dport ${s_port}"
cd  /home/ops/conf/
iptables-save >rc.firewall.txt
grep  "dport ${s_port} -j"  rc.firewall.txt ||  sed  -i  "/-A INPUT -j REJECT --reject-with icmp-host-prohibited/i\-A INPUT -p tcp -m state --state NEW -m tcp --dport ${s_port} -j ACCEPT"  rc.firewall.txt
iptables-restore rc.firewall.txt
echo  "[-] iptables status:"
iptables -nL
 
echo  "[-] check it before running 'service iptables save'"
 
 
在其中一台上测试执行这个sls:
[root@master salt] # salt 'test1.company.com' state.sls firewall
test1.company.com:
----------
           ID:  /home/ops/bin/firewall_add_dport .sh
     Function:  file .managed
       Result: True
      Comment: File  /home/ops/bin/firewall_add_dport .sh is  in  the correct state
      Started: 17:49:51.332723
     Duration: 326.191 ms
      Changes:   
----------
           ID: iptables-add-dport
     Function: cmd.run
         Name:  /bin/bash  /home/ops/bin/firewall_add_dport .sh
       Result: True
      Comment: Command  "/bin/bash /home/ops/bin/firewall_add_dport.sh"  run
      Started: 17:49:51.659900
     Duration: 30.57 ms
      Changes:   
               ----------
               pid:
                   3945
               retcode:
                   0
               stderr:
                   
               stdout:
                   [-] add dport 10050
                   -A INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT 
                   [-] iptables status:
                   Chain INPUT (policy ACCEPT)
                   target     prot opt  source                destination         
                   ACCEPT     all  --  0.0.0.0 /0             0.0.0.0 /0            state RELATED,ESTABLISHED 
                   ACCEPT     icmp --  0.0.0.0 /0             0.0.0.0 /0           
                   ACCEPT     all  --  0.0.0.0 /0             0.0.0.0 /0           
                   ACCEPT     tcp  --  0.0.0.0 /0             0.0.0.0 /0            state NEW tcp dpt:22 
                   ACCEPT     tcp  --  0.0.0.0 /0             0.0.0.0 /0            state NEW tcp dpt:10050 
                   REJECT     all  --  0.0.0.0 /0             0.0.0.0 /0            reject-with icmp-host-prohibited 
                   
                   Chain FORWARD (policy ACCEPT)
                   target     prot opt  source                destination         
                   REJECT     all  --  0.0.0.0 /0             0.0.0.0 /0            reject-with icmp-host-prohibited 
                   
                   Chain OUTPUT (policy ACCEPT)
                   target     prot opt  source                destination         
                   [-] check it before running  'service iptables save'
 
Summary
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
 
 
 
确认无误后,批量执行:
[root@master salt] # salt '*.company.com' state.sls firewall
 
 
 
 
确认无误,保存防火墙配置:
[root@master salt] # salt '*.company.com' cmd.run 'service iptables save' 
test1.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test2.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test3.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test4.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test5.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test6.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test7.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test8.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]
test9.company.com:
     iptables: Saving firewall rules to  /etc/sysconfig/iptables : [  OK  ]                                                                                         



本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1631029,如需转载请自行联系原作者
相关文章
|
1月前
|
监控 网络协议 安全
华为配置防火墙直连路由器出口实验
华为配置防火墙直连路由器出口实验
|
2月前
|
网络安全 数据中心
百度搜索:蓝易云【Proxmox软件防火墙的配置教程】
现在,你已经完成了Proxmox软件防火墙的配置。请确保你的防火墙规则设置正确,以保护你的Proxmox VE环境免受未经授权的访问和网络攻击。
129 5
|
6月前
|
开发框架 网络协议 Ubuntu
【Linux】配置网络和firewall防火墙(超详细介绍+实战)
【Linux】配置网络和firewall防火墙(超详细介绍+实战)
1353 0
|
9月前
|
网络协议 Linux 网络安全
Centos7 防火墙配置+端口设置
Centos7 防火墙配置+端口设置
210 0
Centos7 防火墙配置+端口设置
|
24天前
|
网络协议 安全 网络安全
二层交换机与防火墙对接上网配置示例
二层交换机指的是仅能够进行二层转发,不能进行三层转发的交换机。也就是说仅支持二层特性,不支持路由等三层特性的交换机。 二层交换机一般部署在接入层,不能作为用户的网关
|
1月前
|
运维 Linux 应用服务中间件
Centos7如何配置firewalld防火墙规则
Centos7如何配置firewalld防火墙规则
50 0
|
5月前
|
网络协议 Linux 网络安全
suse 12 配置ip,dns,网关,hostname,ssh以及关闭防火墙
suse 12 配置ip,dns,网关,hostname,ssh以及关闭防火墙
151 0
|
3月前
|
网络协议 Shell Linux
系统初始化shell,包括:挂载镜像、搭建yum、设置主机名及IP地址和主机名映射、配置动态IP、关闭防火墙和selinux
系统初始化shell,包括:挂载镜像、搭建yum、设置主机名及IP地址和主机名映射、配置动态IP、关闭防火墙和selinux
38 2
|
7月前
|
网络协议 Linux 网络安全
百度搜索:蓝易云【Linux 防火墙配置(iptables和firewalld)详细教程。】
以上只是一些常见的iptables和firewalld命令示例,你可以根据自己的需求进行修改和扩展。请注意,在配置防火墙时务必小心,确保不会阻塞你所需要的合法流量,并确保保存和加载配置以使其永久生效。另外,建议在配置防火墙之前备份现有的防火墙规则以防止意外情况发生。
573 2
|
7月前
|
安全 网络安全 网络虚拟化
有配置云防火墙,那么防火墙自然无法发挥其防护功能
有配置云防火墙,那么防火墙自然无法发挥其防护功能
43 2