Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting dynamic ip on linux and pass to iptables script

Posted on 2013-06-17
3
Medium Priority
?
373 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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 …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

721 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