Determine My IP Address

Posted on 2003-10-27
Last Modified: 2013-12-09

I'm writing a perl script for firewall configuration and I need a way of determining my ip address into a variable.

Ie: The ip address of the person telnetted / ssh'd into the system and running the script.
Question by:Plucka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 62

Expert Comment

ID: 9631988
and which exactly is the address you need???
netstat and ifconfig may become useful when combined with awk
LVL 18

Author Comment

ID: 9632001
As above the IP address of the user running the script.
LVL 62

Expert Comment

ID: 9632830
ahh, so
for remote only:
$ who am i | awk -F" " '// { print $4}'
and same for ssh only:
SSH_CLIENT variable is set by OpenSSH server(disable telnet NOW)
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 62

Expert Comment

ID: 9632839
$ id -p
shows if su has been used...
LVL 18

Author Comment

ID: 9632936

That doesnt work cause my server returns a DNS name in the who am i bit, I used to do that.

From your other post I can get the IP address by doing

netstat -n | grep ''

Which shows me the connection on port 22 to the server This returns.

tcp4       0     20    ESTABLISHED

which using your awk example i can do this.

netstat -n | grep '' | awk -F" " '// { print $5}'

which gives me

So i'm almost there, have little experience with awk, can you help me with the awk command to pull off the last .4895 bit.
LVL 18

Author Comment

ID: 9632955
Ok if I add

netstat -n | grep '' | awk -F" " '// { print $5}' -F"." '// {print $1$2$3$4}'

I get the ip address without the .'s ie ( is shown as 192168160)  
LVL 62

Expert Comment

ID: 9633323
no need for grep:
netstat -n | awk -F" " '/ { print $5}' -F"." '// {print $1.$2.$3.$4}'
LVL 62

Accepted Solution

gheist earned 500 total points
ID: 9633376
now you tell you have only ssh:

echo $SSH_CLIENT | awk '// {print $1}'

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question