Solved

Determine My IP Address

Posted on 2003-10-27
8
14,328 Views
Last Modified: 2013-12-09
Hi,

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.
0
Comment
Question by:Plucka
  • 5
  • 3
8 Comments
 
LVL 61

Expert Comment

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

Author Comment

by:Plucka
ID: 9632001
As above the IP address of the user running the script.
0
 
LVL 61

Expert Comment

by:gheist
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)
0
 
LVL 61

Expert Comment

by:gheist
ID: 9632839
$ id -p
shows if su has been used...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Author Comment

by:Plucka
ID: 9632936
Ok,

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 '192.168.1.11.22'

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

tcp4       0     20  192.168.1.11.22      192.168.1.60.4895    ESTABLISHED

which using your awk example i can do this.

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

which gives me

192.168.1.60.4895

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.
0
 
LVL 18

Author Comment

by:Plucka
ID: 9632955
Ok if I add

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

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

Expert Comment

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

Accepted Solution

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

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now