Operating System - HP-UX
1752749 Members
5057 Online
108789 Solutions
New Discussion юеВ

Re: Display current IP address

 
SOLVED
Go to solution
ust3
Regular Advisor

Display current IP address

I would like to have a shell script that can display the user IP address , I think it is similiar to "who -m | awk -F: '{ print $6}'" , this script is used to display what IP is when the user run the program . thx in advance.
8 REPLIES 8
Peter Nikitka
Honored Contributor

Re: Display current IP address

Hi,

this may lead to a multiple line output, if more than one interface is configured:
netstat -in | awk 'NR>1 && !/lo/ {print $4}'

To get the first entry only:
netstat -in | awk 'NR>1 && !/lo/ {print $4; exit}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
spex
Honored Contributor

Re: Display current IP address

Hello,

You could add the following to /etc/profile or the user's .profile:

export REMOTE_HOST=$(who -mu | awk '{print $8}')
export REMOTE_IP=$(/usr/sbin/arp ${REMOTE_HOST} | awk '{gsub(/[()]/,"",$2)}END{print $2}')

Then just echo ${REMOTE_IP}.

PCS
Robert-Jan Goossens
Honored Contributor

Re: Display current IP address

Hi,

someting like?
------
#!/usr/bin/ksh

unset IPADDRESS

IPADDRESS=$(/usr/bin/who am i -T|awk '{print $9}')
export $IPADDRESS:0.0
------
Regards,
Robert-Jan
A. Clay Stephenson
Acclaimed Contributor

Re: Display current IP address

getip $(hostname)
If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: Display current IP address

In general, it is best to use awk's $NF variable to get the last field. Counting fields (ie, $5 $6, etc) means that a change the format of preceeding data will shift the last field. Something like this:

who -muR | awk '{print $NF}'
or
MYIP=$(who -muR | awk '{print $NF}')


Bill Hassell, sysadmin
Aashique
Honored Contributor

Re: Display current IP address

Hi,
you can use this command:

who -mu|awk '{print $8}'

Thanks & Regards

A. Aashique
Aashique
Honored Contributor

Re: Display current IP address

sorry folks,
Use this command:

who -u|awk '{print $8}'

Thanks & Regards

A. Aashique
Ernesto Cappello
Trusted Contributor
Solution

Re: Display current IP address

Hi ust3, you can try this:

who -u | grep ernesto | awk '{print $8}' | sort | uniq

where "ernesto" is the login_name.

Best regards.
ernesto