Get the IP instead of computer name with "who" command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the IP instead of computer name with "who" command
# 1  
Old 11-24-2011
Get the IP instead of computer name with "who" command

I have two HP Servers, server A and server B.
When I input
Code:
who -a | grep $$

command on server A, in the output I get the IP of the machine who is executing the current process.
But when i execute the same command
Code:
who -a | grep $$

on server B, I get the computer same instead of IP.

Can some one help me in knowing what changes I need to do to get the IP on server B as well.

Thanks
# 2  
Old 11-24-2011
Suppose if this is the output of your
Code:
who -a | grep $$

:
Code:
root     + pts/2        2011-11-23 03:38   .         16097 (my.hostname.xyz)

Then, you can do this work-around to get the IP address.
Code:
ping -c1 `who -a | grep $$ | awk '{print $8}' | sed 's/[()]//g'` | head -1 | awk '{print $2}'

Explanation:
Code:
who -a | grep $$ | awk '{print $8}' | sed 's/[()]//g'

will fetch you 'my.hostname.xyz'

Code:
ping -c1 my.hostname.xyz | head -1

will fetch you
PING 123.123.123.123 (123.123.123.123) 56(84) bytes of data

Piping this output to
Code:
awk '{print $2}'

will give you the required IP address i.e. 123.123.123.123.

I know this is quite some work around. But this would work on my system if I had the same problem. (Though, I'm not using HP-UX)
# 3  
Old 11-24-2011
This is not working as
Code:
who -a | grep $$ | awk '{print $8}'

does not give me complete host-name name.
It give me first 17 letters whereas my host-name is greater than that.
Since complete host-name is not found ping cannot find the host
# 4  
Old 11-24-2011
@bhi, that was just a sample I gave you. You have to manipulate it to suit your requirement. Try to tweak around with sed, awk or cut to get the required output.

May be someone can help if you can print the exact output when you give
Code:
who -a | grep $$

---------- Post updated at 01:39 PM ---------- Previous update was at 01:37 PM ----------

The idea here is to separate the hostname from the output that you get when you say "who -a | grep $$". Once you get the hostname, using that in ping command would display the IP address. Tweak around with ping's output to echo just the IP address.
# 5  
Old 11-24-2011
If you have "host" (dns utility), then use that to resolve hostname to IP address.
Provide an output of who -a

--ahamaed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

6. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

7. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. UNIX for Advanced & Expert Users

The "PS" command was displaying*terminals named as "SYSCON"

Hi, When typing the command ps -fe.the system is showing a process called SYSCON:confused:.... I am not sure what process is that.I hava a script that kills all command staring with 'sys' but i don't want to kill syscon:( since i think it is some system process:confused: .Please help me to find... (1 Reply)
Discussion started by: kiranjose85
1 Replies
Login or Register to Ask a Question