我正在做一个脚本,在我的服务器上添加/etc/iptables.rules
文件样本。
/etc/iptables.rules
*filter
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-N DOCKER
${DROP_INTERFACE}
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
COMMIT
然后,节点JS脚本在${DROP_INTERFACE}
多行之后添加,然后删除标记:
app.js
// retrieve public interface in order to restrict access
var names = _.pluck(results.publicIfaceList, 'name');
var dropInterface = []; // eg : ['eth1', 'eth2"]
for(var i = 0; i < names.length; i++){
// add a new DROP rule
dropInterface.push("-A FORWARD -i "+names[i]+" -j DROP");
}
// for each rules
async.eachSeries(dropInterface, function iterator(item, next){
// add a line after ${DROP_INTERFACE}
var cmd = "sed -i '/${DROP_INTERFACE}/a " + item + "' " + FirewallConstants.IPTABLES_RULES;
bash.execute(cmd, function(output){
next();
});
}, function done(){
// remove the line with ${DROP_INTERFACE}
var cmd = "sed -i '/${DROP_INTERFACE}/d' " + FirewallConstants.IPTABLES_RULES;
bash.execute(cmd, function(output){
cb();
});
});
我还试图一次用多行替换标签:
var names = _.pluck(results.publicIfaceList, 'name');
var dropInterface = [];
for(var i = 0; i < names.length; i++){
dropInterface.push("-A FORWARD -i "+names[i]+" -j DROP");
}
var cmd = "sed -i '/${DROP_INTERFACE}/c\\"+dropInterface.join('\n')+"' /etc/iptables.rules";
bash.execute(cmd, function (output){
cb();
});
我的方法都没有用,我有不同的错误,如:
!DROP_INTERFACE
代替${DROP_INTERFACE}
进行测试,仅使用“!”角色被删除了。${DROP_INTERFACE}
错误发生(或类似的东西)。有没有人有一个使用nodejs脚本中的sed进行多行文本插入的工作示例?
主要目标是将此接口的访问权限放弃到在公共接口上监听的任何服务(0.0.0.0
)。
答案 0 :(得分:1)
为我工作:
# Below require needed only if code is not being used in Rails app
require "active_support/core_ext/hash"
a = {"0" => ["2", "3"], "1" => "4", "3" => "5"}
p a.deep_merge(a) {|_,v,_| v.to_i rescue v.map(&:to_i)}
.transform_keys(&:to_i)
.tap { |h| h.reverse_update (h.keys.min..h.keys.max).zip([nil]).to_h }
.sort {|a,b| a <=> b} # Not really needed
.to_h # Not really needed
#=> {0=>[2, 3], 1=>4, 2=>nil, 3=>5}