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

本文涉及的产品
云防火墙,500元 1000GB
日志服务 SLS,月写入数据量 50GB 1个月
简介:

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,如需转载请自行联系原作者
相关文章
|
9天前
|
网络协议 Ubuntu 网络安全
|
1月前
|
运维 监控 安全
网络管理:防火墙和安全组配置详解
网络管理:防火墙和安全组配置详解
49 1
|
2月前
|
监控 安全 网络安全
防火墙配置与管理技巧深度解析
【8月更文挑战第19天】防火墙的配置与管理是网络安全工作的重中之重。通过明确安全策略、精细的访问控制、日志与监控、更新与维护等配置技巧,以及权限管理、自动化与集成、应急响应计划等管理技巧,可以显著提升防火墙的安全防护能力。然而,网络安全是一个持续的过程,需要不断学习和适应新的威胁和挑战。因此,建议网络安全从业人员保持对新技术和新威胁的关注,不断提升自己的专业技能和应对能力。
|
2月前
|
安全 网络安全 数据安全/隐私保护
手把手教你用eNSP模拟器配置防火墙源NAT
手把手教你用eNSP模拟器配置防火墙源NAT
194 4
|
2月前
|
网络安全
如何用HCL模拟器配置防火墙IRF?
如何用HCL模拟器配置防火墙IRF?
|
2月前
|
安全 Linux 测试技术
在Linux中,如何配置防火墙和安全规则?
在Linux中,如何配置防火墙和安全规则?
|
2月前
|
前端开发 Linux 网络安全
在Linux中,如何配置防火墙?
在Linux中,如何配置防火墙?
|
2月前
|
安全 Linux 应用服务中间件
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
|
1月前
|
机器学习/深度学习 安全 网络协议
Linux防火墙iptables命令管理入门
本文介绍了关于Linux防火墙iptables命令管理入门的教程,涵盖了iptables的基本概念、语法格式、常用参数、基础查询操作以及链和规则管理等内容。
201 73
|
2月前
|
Linux 网络安全
linux关闭方防火墙的命令
linux关闭方防火墙的命令
63 2