LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2005, 08:09 AM   #1
km4hr
Member
 
Registered: Aug 2003
Posts: 39

Rep: Reputation: 15
command to retrieve IP address?


I'm working on a bash script. I need a statement that gets the local computer's IP address. I see that 'ifconfig eth0' returns a bunch of stuff that contains the IP address but I then have to use grep/awk/sed or whatever to filter out just the address. Is there an easier way?
xyz
 
Old 01-17-2005, 08:36 AM   #2
Frustin
Member
 
Registered: May 2002
Location: Essex, UK
Distribution: Debian, Redhat, AIX 5L
Posts: 512

Rep: Reputation: 30
/sbin/ifconfig eth0 | grep "inet add" | cut -f2 -d\: | cut -f1 -d" "

there is probably an easier way (there always is).
 
Old 01-17-2005, 08:40 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
If you're going to bash scripts, learn to love grep/awk/sed.

ip addr | grep "init "
 
Old 01-17-2005, 08:43 AM   #4
scowles
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Fedora
Posts: 620

Rep: Reputation: 31
Re: command to retrieve IP address?

Quote:
Originally posted by km4hr
I'm working on a bash script. I need a statement that gets the local computer's IP address. I see that 'ifconfig eth0' returns a bunch of stuff that contains the IP address but I then have to use grep/awk/sed or whatever to filter out just the address. Is there an easier way?
xyz
Not that I'm aware of. If there was a command that returned "just" the IP address, it is probably a shell script that called ifconfig | grep | sed etc...

FWIW: If you are new to shell scripting and familiar with programming - shell scripts support functions.

Example:
Code:
!#/bin/bash

# Function to extract IP address
extract_ip () {
     ifconfig eth0 | grep | sed | etc...
}

# Set shell variable IP to ip address
IP=`extract_ip`
 
Old 01-17-2005, 10:17 AM   #5
km4hr
Member
 
Registered: Aug 2003
Posts: 39

Original Poster
Rep: Reputation: 15
Good info guys. Very good info! Thank you, very much.
 
  


Reply



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
Retrieve Window ID in command line.. joely2k Linux - General 3 11-26-2005 11:44 AM
what is the command to retrieve words between { and } raj_thota Programming 2 11-24-2005 12:13 PM
Command to find my IP address? mwheeler Linux - Networking 6 11-20-2004 05:26 AM
Command to find workstation IP address minno44 Linux - Newbie 2 10-08-2004 03:11 PM
How can I retrieve the IP address of an interface? kobilevi Linux - Networking 2 05-15-2001 07:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration