Pablo
Pablo

Reputation: 29519

use external file with variables

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

Upvotes: 2

Views: 3201

Answers (3)

gavenkoa
gavenkoa

Reputation: 48813

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

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

Upvotes: 2

freiheit
freiheit

Reputation: 5054

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.

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 360143

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)")

Upvotes: 1

Related Questions