2

The following is iptable save file, which I modified by setting some variables like you see below.

-A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j REJECT

I also have a bash script which is defining this variables and should call iptables-restore with the save file above.

#!/bin/sh

EXTIF="eth0"
INTIF="eth1"

INTIP="192.168.0.1/32"
EXTIP=$(/sbin/ip addr show dev "$EXTIF" | perl -lne 'if(/inet (\S+)/){print$1;last}');

UNIVERSE="0.0.0.0/0"
INTNET="192.168.0.1/24"

Now I need to use

/sbin/iptables-restore <the content of iptables save file>

in bash script and somehow insert the text file on top to this script, so the variables will be initialized. Is there any way to do that?

UPDATE: even tried this

/sbin/iptables-restore -v <<-EOF;

$(</etc/test.txt)

EOF
1
  • Reposted here. Commented Mar 14, 2011 at 4:19

3 Answers 3

2

Something like this:

while read line; do eval "echo ${line}"; done < iptables.save.file | /sbin/iptables-restore -v

or more nicely formatted:

while read line
  do eval "echo ${line}"
done < iptables.save.file | /sbin/iptables-restore -v

The eval of a string forces the variable expansion stuff.

Sign up to request clarification or add additional context in comments.

Comments

2

Use . (dot) char to include one shell script to another:

  #!/bin/sh
  . /path/to/another/script

3 Comments

The first line of your example is confusing.
That probably should be #!/bin/sh
the text within script is not another script but just data, containing $variables.
1

In your shell script:

. /path/to/variable-definitions
/sbin/iptables-restore < $(eval echo "$(</path/to/template-file)")

or possibly

/sbin/iptables-restore < <(eval echo "$(</path/to/template-file)")

10 Comments

doesn not really work, it is still waiting for user input from stdin
@Michael: Can you edit your question to show more specifically what it is you're trying to do and what the steps to achieve it are? There seems to be something missing in your question.
@Dennis: actually it's quite simple what I want. Just to read data and put into certain position. Guess I will have to use EOF markers...
@Michael: I still don't see how the first line (the one that starts with -A) comes into play. Normally, you'd use iptables-restore < saved-file.
@Dennis: but in saved-file you can not specify shell variables... I've modified the saved-file and put some variables wihch I needed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.