Solved

getting dynamic ip on linux and pass to iptables script

Posted on 2013-06-17
3
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE 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 38

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

627 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