Use awk to figure out your ip address from ifconfig for a bash script

Objective:

Rewrite the hosts file so the host itself can use it’s external address by name.

Solution:

Find out the current IP address from ifconfig.

echo ” - `/sbin/ifconfig|awk -F “:” ‘/inet addr/{split($2,a,\” \”);print a[1]}’|head -n1` \
host host@rootninja.com”

Write out the basic stuff in hosts that doesn’t change. Just use whatever’s currently there and hardcode it below. Depending on your needs, this could be anything.

echo “127.0.0.1 localhost.localdomain localhost localhost” > /etc/hosts
echo “::1 localhost6.localdomain6 localhost6″ >> /etc/hosts

Write out the current IP address.

echo “`/sbin/ifconfig|awk -F “:” ‘/inet addr/{split($2,a,\” \”);print a[1]}’|head -n1` \
host host@rootninja.com” >> /etc/hosts

99% of the time you can just use the local address to talk to yourself. This is just an example to show you how wrangle the ip address using awk.

Posted by admica   @   3 June 2010

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Recover files when lsof wont help you
Next Post
Lame Copy and Paste Hijacking »
Powered by Wordpress   |   Lunated designed by ZenVerse