Parse Txt File Of Cidr And Turn Into IPFW Rules

Hello,

I would like to parse this file http://www.okean.com/koreacidr.txt.

what I would like it to do is take the fifth line which is 58.29.0.0/16
and have the script place it in another txt file or rtf file that says

sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in
sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out

or

do shell script “sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in”
do shell script “sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out”
as I’m trying to create a applescript for my ipfw rule sets

then do the same thing over and over with all cidrs in the text file

is this possible?

Rather then just parsing the online txt file, I would rather just copy the txt for website and place it in a txt on my hardrive for parsing, How might I go about this script

Not sure if this is exactly what you’re looking for, virustrapp ” but it might help to kick things off.

The following script should make a new folder according to a user-defined name and location, and then fill it with individual files ” one for each relevant line from the webpage, and one containing the entire text:


-- initial values (modify as required) --

set s to "http://www.okean.com/koreacidr.txt"
set f to path to desktop
set t1 to "do shell script \"sudo ipfw add 00001 deny ip from "
set t2 to " to any in\"" & return & "do shell script \"sudo ipfw add 00002 deny ip from any to "
set t3 to " out\""

-- /initial values --

to |write text| from t to p
	try
		set r to open for access p & ".txt" with write permission
		set eof r to 0
		write t to r
	end try
	try
		close access r
	end try
end |write text|

set t to do shell script "curl " & quoted form of s
set n to text 2 thru end of t's paragraph 1
set l to paragraphs 5 thru end of t
set text item delimiters to ":"
tell (choose file name with prompt "Choose a folder for the files:" default name n default location f) as Unicode text
	set n to text item -1
	set f to text 1 thru text item -2
end tell
tell application "Finder" to set f to (make new folder at folder f with properties {name:n}) as Unicode text
|write text| from t to f & "¢ " & n
set text item delimiters to space
repeat with i in l
	tell i's text item 1 to my (|write text| from t1 & it & t2 & it & t3 to f & it)
end repeat
set text item delimiters to {""}
tell application "Finder"
	activate
	tell folder f
		set container window's current view to list view
		open
	end tell
end tell

This script is doing everything I wanted except for lots of txts files being created. I want everything in one txt file instead of each line in an individual txt file.

You may of misunderstood or I explained wrong but , instead of placing many txt files in folder with this text below
sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in
sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out

I would rather have them in one file looking like this so its easier to copy into applescript
sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in
sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out
sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in
sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out
sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in
sudo ipfw add 00002 deny ip from any to 58.29.0.0/16 out

hope this helps

Btw when looking at the script snippet
set t1 to “do shell script "sudo ipfw add 00001 deny ip from "
set t2 to " to any in"” & return & “do shell script "sudo ipfw add 00002 deny ip from any to "
set t3 to " out"”

then actually executing the script, shouldn’t is also be writingv “do shell script “sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in””
instead of sudo ipfw add 00001 deny ip from 58.29.0.0/16 to any in

That simplifies things considerably.

Try something like this:


property source_page : "http://www.okean.com/koreacidr.txt"
property file_name : "IPFW Rules.txt"
property target_folder : path to desktop

property text1 : "sudo ipfw add 00001 deny ip from "
property text2 : " to any in" & return & "sudo ipfw add 00002 deny ip from any to "
property text3 : " out"

set data_list to paragraphs 5 thru end of (do shell script "curl " & quoted form of source_page)
set file_path to (choose file name default name file_name default location target_folder ¬
	with prompt "Confirm the name and location for the file:") as Unicode text
set text item delimiters to space
repeat with i in data_list
	tell i's text item 1 to set i's contents to text1 & it & text2 & it & text3
end repeat
set text item delimiters to return
set final_text to data_list as text
set text item delimiters to {""}
try
	set file_ref to open for access file_path with write permission
	set eof file_ref to 0
	write final_text to file_ref
	close access file_ref
	tell application "Finder" to open file_path
on error
	try
		close access file_ref
	end try
end try


Can I buy you a beer or case or two Seriously, You have saved me so so soo so so so so so sooooo much time.

Thank you. Thank you soo much. I appreciate you help with making my life easier

:lol: I’m just glad we were able to help, virustrapp.