search and add same line modified in iptables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and add same line modified in iptables
# 1  
Old 07-03-2008
search and add same line modified in iptables

I have in iptables file the following:


-A RUN -p tcp -m tcp -s ipaddress1 --dport xxx --syn -j ACCEPT
-A RUN -p tcp -m tcp -s ipaddress2 --dport xxx --syn -j ACCEPT

And I want to add for each ipaddress1 and ipaddress2 another IP address
Like this:

-A RUN -p tcp -m tcp -s ipaddress1 --dport xxx --syn -j ACCEPT
-A RUN -p tcp -m tcp -s ipaddress2 --dport xxx --syn -j ACCEPT
-A RUN -p tcp -m tcp -s ipaddress3 --dport xxx --syn -j ACCEPT

Because I have multiple services and definitions I have to search for ipaddress1 and ipaddress2 to add the third IP.

I try to do with sed but no success.

Thanks.
# 2  
Old 07-03-2008
Code:
awk '{print } /ipaddress2/ { print "-A RUN -p tcp -m tcp -s ipaddress3 --dport xxx --syn -j ACCEPT" }' inputfile > outputfile

# 3  
Old 07-04-2008
Solved

It's SOLVED!!!
I have recieved some help with this.
In the begining I have to find 2 IP's but in the end only 1 match helps.

Here's the code

#!/bin/bash
awk 'BEGIN{ FS=" "}
{
value=0
for (i = 1; i <= NF; i = i + 1){
if ( $i ~ /IPADDRESS1/ ){
printf( "%s ", "IPADDRESS3" );
value=1;
}
else {
if ( $i == "COMMIT" ) {
printf( "%s", $i );

}
else {
printf( "%s ", $i );
}
}
} printf( "\n" );
if ( value == 1 ) {
printf ( "%s\n", $0 );
value=0;
}

}' /etc/sysconfig/iptables

I have to add the commit if because if you leave a blank space after COMMIT iptables gives you a sintax error.

Thanks for you reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

2. UNIX for Advanced & Expert Users

Need to search for keywords within files modified at a certain time

I have a huge list of files in an Unix directory (around 10000 files). I need to be able to search for a certain keyword only within files that are modified between certain date and time, say for e.g 2012-08-20 12:30 to 2012-08-20 12:40 Can someone let me know what would be the fastest way... (10 Replies)
Discussion started by: virtual123
10 Replies

3. Shell Programming and Scripting

search for string and add the second line below

Hi there, i have an /etc/hosts file that is organised in sections, like this # # Oracle Servers # 1.1.1.1 boxa 2.2.2.2 boxb 9.9.9.9 boxj # # Prod Sybase Servers # 6.6.6.6 boxt 4.4.4.4 boxz I am just trying to write a line of code that will ill be able to pass the comment block... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

4. Shell Programming and Scripting

Search a pattern and add new line below

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

5. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

search last modified file and process it

Hello, trying to build a shell script that - searches in defined location for similar files (eg. containing pattern in file name) - Identifies last modified - stores name in variable for processing Thanks a lot (4 Replies)
Discussion started by: alice07
4 Replies

8. Shell Programming and Scripting

How can i search a file which has been created or modified in last five minutes

Hi Can some one please help me How can i search a file which has been created or modified in last five minutes I have used the command find . -mmin -5 and it does not work i get an error -mmin is bad option Please help Much regards Tarun (2 Replies)
Discussion started by: tarundeepdhawan
2 Replies

9. Shell Programming and Scripting

search by modified date

Hello, How can i search for all the files in a particular directory which are not updated from past 5 days. Thanks, Sateesh (4 Replies)
Discussion started by: kotasateesh
4 Replies

10. Shell Programming and Scripting

Script needs to be modified - Each 5 Rows to be joined in single line with comma (,)

Hi All, I'm using the following script to produce a result: #!/bin/sh awk ' $0 ~ /\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+Interface\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+/ { match_str="YES"; line_cnt=0; next; } { if((line_cnt < 5) && ( match_str=="YES")) { print $0; line_cnt += 1; } else... (0 Replies)
Discussion started by: ntgobinath
0 Replies
Login or Register to Ask a Question