Solved

getting dynamic ip on linux and pass to iptables script

Posted on 2013-06-17
3
341 Views
Last Modified: 2013-06-17
Hi,

I need to pass IP to iptables script and tried below method, and it works:
/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

However, when I tried to use it in my script:
#!/bin/sh
IPT="/sbin/iptables"
IF=eth0
ETH0=/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

$IPT -A INPUT -s 0/0 -i $IF -d $ETH0 -p TCP --dport 22 -m state --state NEW -j ACCEPT

I keep getting:
eth0: not found

I missed out anything? Is this even the right way?

Thanks in advanced!
0
Comment
Question by:chainfear
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 39252659
Try putting your commands in the script either between back quotes or evaluating the result like this:

IPT=`/sbin/iptables`

or

IPT=$(/sbin/iptables)

In your case:

ETH0=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`

or

ETH0=$(/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
0
 

Author Comment

by:chainfear
ID: 39252763
thanks!!
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39252830
You're welcome :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now