SED inserting iptables rules in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED inserting iptables rules in while loop
# 1  
Old 12-21-2009
Question SED inserting iptables rules in while loop

I'm trying to insert multiple new lines of text into an iptables script using sed in a while loop. I'm not sure if this is the most effective way. Searching the forums has helped me come up with a good beginning but it's not 100%. I'd like it to search out a unique line in my current iptables file and add new rules above it. The problem now is that it only ends up inserting one new line instead of the 20 it should (based on the $BOGON_LIST).

Code:
        while read bogonline ;do
                sed '/--state ESTABLISHED,RELATED -j ACCEPT/ i\-A RH-Firewall-1-INPUT -s '"${bogonline}"' -j DROP' $IPT_SCRIPT > new_iptables
        done < $BOGON_LIST

The $BOGON_LIST is a file with ip addresses on a line by themselves like so:
IP
IP
IP
# 2  
Old 12-21-2009
Code:
${bogonline}=`echo ${bogonline}|sed '/--state ESTABLISHED,RELATED -j ACCEPT/ i\-A RH-Firewall-1-INPUT -s '" `

# 3  
Old 12-22-2009
rdcwayx, Thanks for the reply. I think I need a bit more hand holding. I attempted to plug your solution into my script and finish it a number of different ways, but no luck. Banged my head against the screen for an hour. Lots of errors (IP = No such file or directory). There are some fundamentals I am missing here, and I could use a hand. Could you elaborate a bit more? TIA.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a long string (readable in sed)

Hi everyone, I am trying to insert a single very long string as the first line of a file, So the following sed commands does what I want; sed '1i\"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"' file.txt Think that all the... (3 Replies)
Discussion started by: hayreter
3 Replies

2. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

3. Shell Programming and Scripting

inserting line at particular linenumber using sed

Could anyone tell me how to insert a line at a particular line number using sed. sed '2i\newtext' filename I have tried the above command .But it is not working. I am using tcsh shell .Please help. Double post, continued here (0 Replies)
Discussion started by: maitree
0 Replies

4. Shell Programming and Scripting

Inserting text with SED

Hi guys, I need to insert @test.com after each entry in my .txt file. 1 2 3 4 1@test.com 2@test.com 3@test.com 4@test.com Tried to use cat test.txt |sed 's/$/@test.com/'but it does this instead: 1 @test.com 2 (6 Replies)
Discussion started by: spirm8
6 Replies

5. Shell Programming and Scripting

problem with sed for inserting command

Hello, i use sed '/good/ a\INSERT' 1.txt command for insert a text on my file on linux butit does not work see the output : # sed '/good/ a\INSERT' 1.txt if_test=iso ifup=eth0 Hello World Bye now good INSERT # cat 1.txt if_test=iso ifup=eth0 Hello World Bye now (1 Reply)
Discussion started by: blackmetal
1 Replies

6. Shell Programming and Scripting

Non-inserting backslash in sed statement

#!/bin/bash wget -O tmp.tmp "YouTube - Pretty Woman- Vivian's Goes Shopping!" temp=`grep 'one&video_id=' tmp.tmp | sed "s/.*one&video_id=\(.*\)'\;.*/\1/"` temp="http://www.youtube.com/get_video?video_id=$temp" temp=`echo $temp|sed -n "s/!/\\!/p"` echo " -O $filename \"$temp\"" Output:... (3 Replies)
Discussion started by: kds1398
3 Replies

7. Shell Programming and Scripting

Inserting New Line in File using Sed ??

Dear All, I have a file called football where i have a list of 11 players each on different lines. I wish to add a name of another player on the first line. I have created a file called footballscript in vi writing the following sed command to achieve this ... cat football | sed -e '1 i\... (4 Replies)
Discussion started by: Mary_xxx
4 Replies

8. Shell Programming and Scripting

Inserting Lines between data sets using SED?

Hello all and thanks in advance! What I'm looking to do is insert a blank line, anytime the first 9 characters of a given line don't match the first 9 characters of the previous line. i.e. Convert the data set 1 45 64 89 1 89 69 235 2 89 234 67 2 56 90... (1 Reply)
Discussion started by: selkirk
1 Replies

9. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies

10. Shell Programming and Scripting

Inserting a carriage rtn in a sed cmd

I suppose this is a silly newbie Q, but I have a set of text I am inserting into a file using a sed cmd and before I insert, I want to ask the sed operation to move to add a carriage return first, and then place my new line. Is there scope within the sed command to do this - i have tried "\n"... (6 Replies)
Discussion started by: sirtrancealot
6 Replies
Login or Register to Ask a Question