Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grap IP of Current Host 2

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
For a Solaris 8 workstation using csh I need to have a script that launches an application. This app requires the host systems IP that is launching it so...

How can I grab and use the IP of the current host in a script.

Example line: /dirx/appname <ip>

Thanks! Michael
 
HOSTNAME=`hostname`
MYIP=`cat /etc/hosts | grep ${HOSTNAME} | awk -F' ' '{print $1}'`

appname ${MYIP}
 
above assumes a couple of things:
1) you're on a single-nic box, no multihoming
2) /etc/hosts is &quot;correct&quot; - even with DNS operating, /etc/hosts should at least contain the localhost entry and &quot;my&quot; entry
 
Michael:

This is one way to do it. Sometime ago I had a similar requirement under Solaris 7. I did a self ping against the server. This example prints the IP address of server charlie:

# all on one line
ping -s charlie 64 1|head -n 1|sed -e 's,^[^(]*(,,'
-e 's,).*$,,' -e 1q

The sed looks nasty, but it's used to grap the address which is surrounded by parenthesis ().

Regards,

Ed
 
Code:
ifconfig -a | awk '/inet/ {print $2}'
works for me, and returns all ip's you may need to modify slightly for linux systems, i'm working on a solaris box.
 
Chapter11
HOSTNAME=`hostname`
DO NOT USE THAT:
MYIP=`cat /etc/hosts | grep ${HOSTNAME} | awk -F' ' '{print $1}'`
USE:
awk &quot;/$HOSTNAME/{print $1};&quot;

Michael:
sed -e 's/^.*(\(.*\)).*/\1/'
you don't need 'head' if you quit sed after the first line '1q'

----------------
on solaris:
getent hosts `hostname` | sed -e 's/[a_space+a_tab].*//'
:)
vox clamantis in deserto.
 
All very good suggestions.
You guys are great.

Thanks :) Michael
 
add...
| tail -1

to Jad's suggestion to simplify even further, therwise you will pickup 127.0.0.1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top