Adding new iptables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding new iptables
# 1  
Old 10-07-2008
Adding new iptables

Hi, I basically want to create a shell script that reads in the /etc/sysconfig/iptables. When it sees the line "-A INPUT -j REJECT --reject-with icmp-host-prohibited" it will create a new line before it and add in the line from another file which will have "-A INPUT -p udp -m udp --dport 27020 -j ACCEPT"
So it will look like:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp -m udp --dport 27020 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

Last edited by SpaceY; 10-07-2008 at 01:28 PM..
# 2  
Old 10-08-2008
Should be something like:

Code:
line=`grep your line from the file here`

awk -v var="$line" '/-A INPUT -j REJECT --reject-with icmp-host-prohibited/{print var}1' /etc/sysconfig/iptables

Regards
# 3  
Old 10-08-2008
Or with sed:

Code:
sed '/-A INPUT -j REJECT --reject-with icmp-host-prohibited/i\
-A INPUT -p udp -m udp --dport 27020 -j ACCEPT' file >newfile

There are different dialects of sed so you might need to experiment a bit -- the backslash might or might not be necessary.
# 4  
Old 10-08-2008
Wierd...since I tried both examples and still didnt work Smilie
# 5  
Old 10-09-2008
Quote:
Originally Posted by SpaceY
Wierd...since I tried both examples and still didnt work Smilie
Did you get errors, no output or wrong output?

Regards
# 6  
Old 10-09-2008
I'm guessing you might have multiple spaces in your input file, but we can't see those because you didn't use code tags -- please post again with those tags around the sample so we can see the spaces if you can't solve this on your own.
# 7  
Old 10-09-2008
Yup, in both cases the output is the same as the original iptables file with no modification being done into a new file.
Code:
line=`cat ports | grep 27020`
*** the cat grabs the line -A INPUT -p udp -m udp --dport 27020 -j ACCEPT ***
awk -v var="$line" '/-A INPUT -j REJECT --reject-with icmp-host-prohibited/{print var}1' iptables >newiptables

Code:
sed '/-A INPUT -j REJECT --reject-with icmp-host-prohibited/i\
-A INPUT -p udp -m udp --dport 27020 -j ACCEPT' iptables >newiptables

This seems to be interesting. you may be right that it could be a spaces issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. UNIX for Dummies Questions & Answers

Adding

my shell script: #!/bin/ksh date +%d > /tmp/day.log day=`tail /tmp/day.log` ############################ for example: date +%d shows me 05 i want to add 14 days to 05 into my above script. bc 5+15 19 but i am not sure how to put into above script. (5 Replies)
Discussion started by: lawsongeek
5 Replies

3. Shell Programming and Scripting

Adding in Awk

Hello, suppose I have a file that consists of a single column of various numbers, as in 12.010 1.0080 1.0080 0.8780 0.1350 0.0000 -0.4157 0.2719 How can I use AWK (or equivalent) to add the numbers of two specific lines? I want to sum, for example, the first with the fifth, the second... (4 Replies)
Discussion started by: Leo_Boon
4 Replies

4. AIX

adding new ip

Hi Admins, I was told to add new ip,mask and gateway to my 3rd nic.so i prepared a plan doing the same via smitty. now i need to know do i plumb and unplumb before adding ip. plz suggest Regards newaix (1 Reply)
Discussion started by: newaix
1 Replies

5. Shell Programming and Scripting

Adding Variables

Hi. I have a for loop that I use to extract integer values in a shell script (ksh). Now, I would like to add the values. My preference, from my c programming days, would be to do something like the commented out line below in the for loop. However, this is not recognised. So I use the line... (2 Replies)
Discussion started by: mikem22
2 Replies

6. Shell Programming and Scripting

Adding new field

Hello, I have a main file with IP addresses like this: Erisim var,100,172.17.241.5,4006,60,IS0799,TCP/IP Erisim var,1003,172.17.140.4,4004,60,IS2156,TCP/IP Erisim var,1004,172.17.140.5,4002,60,IS2636,TCP/IP Erisim var,1005,172.17.140.5,4004,60,IS2436,TCP/IP Erisim... (8 Replies)
Discussion started by: Spunkerspawn
8 Replies

7. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

8. UNIX for Dummies Questions & Answers

adding

Hi All i need a add recored like DateOfDU2=245,Time=00326 (in milli secounds ) DateOfDU2=245,Time=00347 DateOfDU2=245,Time=00258 DateOfDU2=246,Time=00325 DateOfDU2=246,Time=00408 DateOfDU2=246,Time=00257 DateOfDU2=247,Time=00037 DateOfDU2=247,Time=00417 DateOfDU2=247,Time=00420... (1 Reply)
Discussion started by: nalakaatslt
1 Replies

9. UNIX for Advanced & Expert Users

adding zero's

Hi I am comparing two files, 100th column have formatting issue i mean 1 file have scale 4 and anothe file scale 2 ,if scale 2 need to add two zeros.Please any idea how to add two zers to 100th coulmn if scale is 2 file 1 .................1234.2000 file2 ................1234.20 ... (3 Replies)
Discussion started by: mohan705
3 Replies

10. UNIX for Dummies Questions & Answers

Adding a new HDD

I am adding a new HDD to a Unix Sco Release 5 webserver. I consider myself a windows pro. However, growing up in the late 90's means I have little Unix knowledge. I know the HDD has to be mounted and formatted correctly. Can anyone give me any advice on this? A dummy's guide to installing a... (5 Replies)
Discussion started by: jeffreydavisjr
5 Replies
Login or Register to Ask a Question