The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed Help in Updating something only in one particular file. bisla.yogender Shell Programming and Scripting 4 03-24-2008 11:48 AM
updating kde 3.5 to 4.0 revolver UNIX for Dummies Questions & Answers 2 01-25-2008 11:04 PM
Updating GCC in Linux mimino High Level Programming 2 08-24-2005 06:44 AM
updating ssa chosie AIX 1 06-03-2005 04:39 AM
Need help to access/mount so to access folder/files on a Remote System using Linux OS S.Vishwanath UNIX for Dummies Questions & Answers 2 07-30-2001 08:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-28-2008
manouche manouche is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 13
sed used for updating firewall to accept

Here is my situation. You can make all kinds of comments about how I am doing it and why from the networking standpoint, but I am really only looking for comments on how to make it work. Of course, any other ideas for how to do this would be welcome.

I have an iptables firewall that allows only specific ips in to the ftp port. I have a user who needs ftp access but works from a variety of locations and thus, the ip is constantly changing.

So, I read about "port knocking" and my idea is an offshoot of that.

I created a web page that captures the users ip address at the time and writes it to a file. I have two other text files that include the other parts of the iptables string needed for the firewall.

Here is file1.txt:

/sbin/iptables -A INPUT -p tcp -s

Here is ip.txt: (actual ip address masked from forum)

my.ip.my.ip

Here is file 3.txt:

--destination-port 21 -j ACCEPT

So, the idea is that I capture the ip into ip.txt, then cat the three files like this and use sed to put them back on one line, separated by spaces.

cat 1.txt ip.txt 3.txt | sed -n -e ":a" -e "$ s/\n//gp;N;b a" > final.txt

So, now I have the file final.txt that looks like this:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

The next step is to modify the firewall. I could rename this file to updatefirewall.sh and use this file as a script to modify the firewall like this:

./updatefirewall.sh or something like that.

Or, I could use sed or some other utility to do a search and replace/substitute on the actual firewall script that I have in place. The comment for that particular entry has a distinguishing character at the end of it, so I could search for the string and do a replace with the new string. I came up with the following, which looks for the end of the comment (#) and a new line (\n), and then an ip address and replaces it with "showboat".

sed 'N;s/#\n/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/showboat/'

That was just for a test. I actually need to replace it with the line:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

which would be read from the text file final.txt.

So, after all that, my question is, is there a way for sed to read in a string from a file, or would I really need to create a sed script with variables and such?
  #2 (permalink)  
Old 02-28-2008
manouche manouche is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 13
sed used for updating firewall allow ftp from DHCP access

Here is my situation. You can make all kinds of comments about how I am doing it and why from the networking standpoint, but I am really only looking for comments on how to make it work. Of course, any other ideas for how to do this would be welcome.

I have an iptables firewall that allows only specific ips in to the ftp port. I have a user who needs ftp access but works from a variety of locations and thus, the ip is constantly changing.

So, I read about "port knocking" and my idea is an offshoot of that.

I created a web page that captures the users ip address at the time and writes it to a file. I have two other text files that include the other parts of the iptables string needed for the firewall.

Here is file1.txt:

/sbin/iptables -A INPUT -p tcp -s

Here is ip.txt, the ip was captured: (actual ip address masked from forum)

my.ip.my.ip

Here is file 3.txt:

--destination-port 21 -j ACCEPT

So, the idea is that I capture the ip into ip.txt, then cat the three files like this and use sed to put them back on one line, separated by spaces.

cat 1.txt ip.txt 3.txt | sed -n -e ":a" -e "$ s/\n//gp;N;b a" > final.txt

So, now I have the file final.txt that looks like this:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

The next step is to modify the firewall. I could rename this file to updatefirewall.sh and use this file as a script to modify the firewall like this:

./updatefirewall.sh or something like that.

Or, I could use sed or some other utility to do a search and replace/substitute on the actual firewall script that I have in place. The comment for that particular entry has a distinguishing character at the end of it, so I could search for the string and do a replace with the new string. I came up with the following, which looks for the end of the comment (#) and a new line (\n), and then an ip address and replaces it with "showboat".

sed 'N;s/#\n/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/showboat/'

That was just for a test. It seems to only work if I provide the entire string to replace. I actually need to replace it with the line:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

which would be read from the text file final.txt.

So, after all that, my question is, is there a way for sed to read in a string from a file, or would I really need to create a sed script with variables and such?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0