LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-14-2004, 08:34 AM   #1
fr0st
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

Fr0st-
 
Old 01-14-2004, 08:52 AM   #2
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

Rep: Reputation: 51
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
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
This should work:
$ hostname -i
 
Old 01-14-2004, 06:22 PM   #4
stoe
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 getip.py, chmod it and your good to go. so, in your bash script you could do something like

Code:
IP=`chkip.py`
to get the address.

Code:
#!/usr/bin/python
from types import *
from httplib import *
import re
import sys

url = "checkip.dyndns.org"
path = "/"

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

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

if isinstance(match, NoneType):
    sys.exit(1)

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


Reply


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


All times are GMT -5. The time now is 04:25 PM.

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