用于防火墙的bash脚本占位符 [英] Placeholders in bash script for firewall

查看:309
本文介绍了用于防火墙的bash脚本占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写函数,这将使得:


  1. 从循环中的文件中读取文本



    $ b
  2. 执行命令

  3. 我有一个命令

     用于`grep -v ^#/etc/rc.d/b bad_input.ip`中的BAD_INPUT_IP;做
    iptables -I INPUT -s $ BAD_INPUT_IP -j DROP
    完成

    我想重新建立这个函数:

    pre $函数go_loop(){
    为`grep -v ^#中的BAD_INPUT_IP $ 1`;做
    iptables -I INPUT -s $ BAD_INPUT_IP -j DROP
    完成
    }

    go_loop/etc/rc.d/bad_input.ip

    我做的任务的第一部分。一切都好。但是如何做这个命令的占位符?
    $ b $ iptable -I INPUT -s $ BAD_INPUT_IP -j DROP



    我会有很多不同的命令,我将需要一个循环。你可以帮我吗 ?感谢。

    解决方案

    在运行函数之前设置变量

    <$对于`grep -v ^#$ file`中的BAD_INPUT_IP,函数go_loop(){
    ; pre>
    iptables -I INPUT -s $ BAD_INPUT_IP -j DROP
    完成
    }

    file =/ etc / rc.d / bad_input.ip
    go_loop


    i want to write function, that will make:

    1. reading text from file in a loop

    2. executing command

    I have a command

    for BAD_INPUT_IP in `grep -v ^# /etc/rc.d/bad_input.ip`; do
    iptables -I INPUT -s $BAD_INPUT_IP -j DROP
    done
    

    I want to rebuild this to function:

    function go_loop (){
    for BAD_INPUT_IP in `grep -v ^# $1`; do
            iptables -I INPUT -s $BAD_INPUT_IP -j DROP
    done
    }
    
    go_loop "/etc/rc.d/bad_input.ip"
    

    First part of task i made. Everything is ok. But how to make placeholder for this command ?

    iptables -I INPUT -s $BAD_INPUT_IP -j DROP

    I will have a lot of different commands, where i will need a loop. Can you help me ? Thanks.

    解决方案

    Set the variable before running going to the function

    function go_loop (){
    for BAD_INPUT_IP in `grep -v ^# $file`; do
            iptables -I INPUT -s $BAD_INPUT_IP -j DROP
    done
    }
    
    file="/etc/rc.d/bad_input.ip"
    go_loop
    

    这篇关于用于防火墙的bash脚本占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆