Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to determine "my" IP address on unix

Status
Not open for further replies.

monk2021

Programmer
Feb 16, 2005
2
US
I have a need to be able to determine my IP address on unix. When you login, the environment variable DISPLAY is set to "my" IP address. But, when you “rlogin”, “telnet”, etc, to another box, the DISPLAY variable is not set. I have investigated several commands including “netstat” and “ifconfig”, but none returned my IP address. “ifconfig” might be able to return my IP address, but I am not privileged to use all options.

I searched the internet for a solution. All “solutions” returned the host IP address or used variations of “ifconfig” that I am privileged to use.

The reason for the need is Tcl/Tk uses the DISPLAY environment variable to determine where to send the GUI output. When I login, I can manual determine my IP address. Then when I “rlogin” to another box, I can manually set the DISPLAY environment variable. This works fine for me, but the users, who are not as familiar with UNIX, will have a problem.

The solution can be in the form of a command, utility, or a programming method.
 
Open a socket.
Connect to a remote host.
Use fconfigure -sockname, -peername,etc..to
determine your address.

Otherwise: man ioctl. Stevens "Unix Network
Programming" has a great example of obtaining
interface information portably in C.
 
I must have missed something, when I do this:

set theClient [socket localhost 80]
set myIP [fconfigure $theClient -sockname]

"myIP" is the host ip address.

 
You must be.
Code:
set ftpsck [socket xxx 21]
sock3
% fconfigure $ftpsck -sockname
192.168.0.87 xxx 56902
% fconfigure $ftpsck -peername
192.168.0.42 xxx 21
 
try this at a prompt:

nslookup `hostname`

the last line should be your address. Or on my linux box i can use:

dig `hostname -f`

Hope that helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top