Solved

getting dynamic ip on linux and pass to iptables script

Posted on 2013-06-17
3
345 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

896 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

14 Experts available now in Live!

Get 1:1 Help Now