LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2006, 09:41 AM   #1
nopcoder
LQ Newbie
 
Registered: Dec 2005
Posts: 10

Rep: Reputation: 0
iptables script


Hi

I have the following script for network gateway setup. How come I can't run it directly via "sh" BUT I can manually type each line in the terminal and execute them fine?

The script file: firewall.sh
Code:
#!/bin/sh

PATH=/usr/sbin:/sbin:/bin:/usr/bin

#
# delete all existing rules.
#
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X

# Always accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT


# Allow established connections, and those not coming from the outside
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW -i ! eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow outgoing connections from the LAN side.
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

# Masquerade.
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# Don't forward from the outside to the inside.
iptables -A FORWARD -i eth0 -o eth0 -j REJECT

# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward
When tried to run directly:
Code:
# sh firewall.sh

: command not found:
: command not found:
iptables: No chain/target/match by that name
iptables: No chain/target/match by that name
iptables: No chain/target/match by that name
iptables: Table does not exist (do you need to insmod?)
: command not found2:
'ptables v1.2.11: Invalid target name `ACCEPT
Try `iptables -h' or 'iptables --help' for more information.
: command not found5:
: command not found6:
'ptables v1.2.11: Invalid target name `ACCEPT
Try `iptables -h' or 'iptables --help' for more information.
'ptables v1.2.11: Invalid target name `ACCEPT
Try `iptables -h' or 'iptables --help' for more information.
'ptables v1.2.11: Invalid target name `ACCEPT
Try `iptables -h' or 'iptables --help' for more information.
: command not found1:
'ptables v1.2.11: Invalid target name `ACCEPT
Try `iptables -h' or 'iptables --help' for more information.
: command not found4:
'ptables v1.2.11: Invalid target name `MASQUERADE
Try `iptables -h' or 'iptables --help' for more information.
: command not found7:
'ptables v1.2.11: Invalid target name `REJECT
Try `iptables -h' or 'iptables --help' for more information.
: command not found0:
Other info: I am running Debian 3.1 and iptables 1.2.11-10

Last edited by nopcoder; 01-07-2006 at 09:44 AM.
 
Old 01-07-2006, 09:55 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
What editor did you use to create the script?
 
Old 01-07-2006, 11:44 AM   #3
nopcoder
LQ Newbie
 
Registered: Dec 2005
Posts: 10

Original Poster
Rep: Reputation: 0
ahhh problem solved (with help from the author of the script actually). The file had carriage returns, but thx anyway =)
 
Old 01-07-2006, 07:37 PM   #4
dvarius
LQ Newbie
 
Registered: Jan 2006
Posts: 7

Rep: Reputation: 0
So what have you done because I have the same problem
 
Old 01-08-2006, 10:35 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
dvarius,
From nopcoder's last post I would say he created the file on a windows PC. DOS/windows end of line character is different then unix for ASCII text files i.e. <CR><LF> vs <LF>.

If you open the file for editing in linux and see ^M at the end of each line then this is your problem. Here are a couple of methods to correct the problem, Use the dos2unix utility or vim and use the set command i.e set ff=unix and save the file.
 
Old 01-08-2006, 09:54 PM   #6
nopcoder
LQ Newbie
 
Registered: Dec 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Precisly

As michaelk mentioned the file was created in windows (interesting how that os always manages to annoy) and I ran the following command to fix it:
perl -pi.bak -e 's/\r\n/\n/' <insert filename here>

NOTE: please do change <insert filename here> to whatever is appropriate

Last edited by nopcoder; 01-08-2006 at 10:02 PM.
 
Old 01-09-2006, 03:00 AM   #7
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Good practice to never open or create files in windows if there intended to run on a unix/linux box. I had the same trouble with some scripts last year.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
my first iptables script sh1ft Linux - Security 1 02-24-2005 04:17 PM
iptables script BACTRATE Mandriva 1 06-24-2004 05:34 AM
iptables script help radix Linux - Security 6 09-25-2003 02:48 PM
My iptables script is /etc/sysconfig/iptables. How do i make this baby execute on boo ForumKid Linux - General 3 01-22-2002 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration