LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-14-2004, 03:09 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 122

Rep: Reputation: 15
script or cmd to retun my ip address....


Hi,

I need a command or script to return my ip address by interface. I know u can see what it is by ifconfig, but I need just the ip address returned to dump in say...an environment variable, that i can later reference in a script. I tried "hostname -i" but it just returns the loopback address. I need to be able to get ip address for say eth0 or eth1. This would be on a redhat 9.0 box.

Thanks!!
 
Old 03-14-2004, 03:16 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
What about:
/sbin/ifconfig eth0|grep inet|awk {'print $2'}|cut -d":" -f2
 
Old 03-14-2004, 04:02 PM   #3
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 122

Original Poster
Rep: Reputation: 15
Hey that works. THX!! Consider yourself affero'd
 
Old 03-14-2004, 04:05 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Cheers Scrag, good to see it worked.

I never actually thought before but if it was your external IP and you are behind a firewall then you can also use:
lynx -dump http://www.whatismyip.com | awk '/^Your IP is/ { print $4; }'
 
Old 03-18-2004, 12:26 PM   #5
marianop
LQ Newbie
 
Registered: Nov 2003
Location: buenos aires, argentina
Distribution: debian/redhat
Posts: 6

Rep: Reputation: 0
this works too:

#!/bin/bash

lista="hostname1 hostname2"

for a in $lista
do
b=$(host $a | awk '{ print $4 }')
echo $b
done
 
Old 07-05-2009, 10:19 PM   #6
steppnav
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
Unhappy Another weird one...

Why is this so hard? I have 2 wireless nets and 2 NICs hooking the little oracle/freeciv Ubuntu server up. How does the person tell which ip they've hooked to? I put up a telnet server so the Windoze here at home can get to it without installing putty or cygwin. They can start networked freeciv games from a simple menu. Showing which IP they connected to, so they know which one to put in the Windoze freeciv client, should be trivial, but it isn't. I put a variable into the .profile for the captive id/menu I use to give control of freeciv. It contains the Shell PID. Here's the resulting kludge. Where is the 'who -whatipaddresdidIhit' option?


#!/usr/bin/ksh
# ---------------------------------------------------
# This kludge walks up the pid tree looking for the telnetd pid.
# It uses that pid to get the ip with lsof...I had to put setuid
# on lsof for this to work. Blechh!!
# ---------------------------------------------------
# NOTE: for the sed stuff, there is a <space><tab> inside the [ ]
# ---------------------------------------------------
PID_OF_INTEREST=$MyPID
while true
do
if ps -ef | grep "^[^ ][^ ]*[ ][ ]*${PID_OF_INTEREST}[ ][ ]*" | grep 'telnetd.*in.telnetd:' > /dev/null 2>&1
then
break
fi
PID_OF_INTEREST=$(ps -ef | grep "^[^ ][^ ]*[ ][ ]*${PID_OF_INTEREST}[ ][ ]*" | awk '{print $3}')
done

lsof -i4 -n | grep "$PID_OF_INTEREST" |
head -1 |
sed 's/.*[ ]\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\):telnet.*/\1/'
 
  


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
Simple script to copy files, getting \r's at end of cmd ElricM Linux - Newbie 2 07-23-2005 11:21 AM
Change IP address via script ricky_ds Red Hat 2 01-04-2005 10:34 AM
creating a script to ask for a new IP address stellarmarine1 Linux - General 5 09-12-2004 08:08 PM
Bash Script: Getting IP address fr0st Programming 3 01-14-2004 06:22 PM
Script to capture dynamic IP address scon Linux - General 4 02-21-2003 03:30 AM


All times are GMT -5. The time now is 09:42 AM.

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