Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-14-2004, 08:34 AM   #1
LQ Newbie
Registered: Apr 2003
Location: Cheshire - United Kingdom
Distribution: RedHat, SuSE, Slackware, EnGarde
Posts: 11

Rep: Reputation: 0
Bash Script: Getting IP address

Hi Guys,

I'm looking to build a simple bash script that would get the IP address of the machine, and then save that into a variable so that I can use it later on in my script.

I have tried to test which will work by doing the following, but to no avail:

echo $IP
echo $ip

Anyone know how this could be done? Especially if the machine is DHCP.. Your probably thinking 'well why doesn't he just type ifconfig as root?' Simple reason for that is, its going to be used to export a display from another machine to this IP address.. And it all needs to work at the click of a button.. I have it working if I manually input the IP into the script, but that isn't a dooable option..

Any advice, help is much appreciated

Old 01-14-2004, 08:52 AM   #2
Senior Member
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,186

Rep: Reputation: 52
you could ping an address and write the output to a file, and
have awk take the fourth field of the first line.
Old 01-14-2004, 08:54 AM   #3
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
This should work:
$ hostname -i
Old 01-14-2004, 06:22 PM   #4
LQ Newbie
Registered: Jan 2004
Location: ny
Distribution: gentoo
Posts: 8

Rep: Reputation: 0
if you don't mind using python, here's a script I wrote a while ago. It prints your ip address and returns 0, or if something unexpected happens nothing is printed and it returns 1. just copy it into a file such as, chmod it and your good to go. so, in your bash script you could do something like

to get the address.

from types import *
from httplib import *
import re
import sys

url = ""
path = "/"

conn = HTTPConnection(url)
conn.request("GET", path)
resp = conn.getresponse()
str =

regex = re.compile("Current IP Address: [\\d.]+")
match =

if isinstance(match, NoneType):

str = str[match.start():match.end()]
regex = re.compile("[\\d.]+")
match =
str = str[match.start():match.end()]
print str


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
bash: ip address twantrd Programming 3 04-21-2005 08:40 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script to detect scsi address at boot ewto Programming 2 10-20-2003 02:47 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration