Extract time and host IP from 'who' command?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract time and host IP from 'who' command?
# 1  
Old 05-06-2009
Extract time and host IP from 'who' command?

I have been slowly working towards getting a shell window in Cygwin (and/or Gnome and/or KDE) to start up and display the last time it was opened (or sourced) similarly to how the OS X Terminal does so. As close as I've been able to get so far is the 'who' command, but I can't seem to puzzle out how to 'pull out' (extract, is perhaps the better term) what is returned under the TIME and COMMENT columns in a "who -h" command. I presume if I could get the time and comment information I could then write a function into my .bashrc that puts that information at the top of a newly-opened (or newly-re-sourced) terminal window, along with my $PS1 line and, when called for, motd.

I've also tried drawing out the time and host Ip from the 'last' command, with equal amounts of $NO_LUCK Smilie

I hope someone more familiar with command return extractions can help me with this.

BZT
# 2  
Old 05-06-2009
Try this as a starting point:
Code:
$ who -m | perl -pe 's/\S+\s+\S+\s+(.*?)$/$1/'
May  6 19:38

or, from remote
Code:
$ who -m | perl -pe 's/\S+\s+\S+\s+(.*?)$/$1/'
May  6 19:30 (192.168.0.31)

# 3  
Old 05-06-2009
It works, now how do I ...

...reformat that string so it appears more like:

Last login: 12:30 from tty1 (:0)
{or remote: 'from 66.189.62.134' (to give an example off the top of my head)}

... which is where I wanted to apply this information?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract date and time from filename?

Hi, I'm totally new in sell script and working with a shell code. I want to extract the date and time from the filenames. The filenames are different but all of them begins with WI_ SCOPE_: WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt WI_SCOPE_BACK_COMPLETE_QUEUE_2017-09-12_15-31-40.txt... (5 Replies)
Discussion started by: Home
5 Replies

2. Shell Programming and Scripting

Grok filter to extract substring from path and add to host field in logstash

Hii, I am reading data from files by defining path as *.log etc, Files names are like app1a_test2_heep.log , cdc2a_test3_heep.log etc How to configure logstash so that the part of string that is string before underscore (app1a, cdc2a..) should be grepped and added to host field and... (7 Replies)
Discussion started by: Ravi Kishore
7 Replies

3. UNIX for Dummies Questions & Answers

Why does SSH store the fingerprints of remote host when connecting for the first time?

I was wondering why does ssh store the fingerprints of remote host when connecting for the first time? I will appreciate a detailed response, can't figure it out. (2 Replies)
Discussion started by: ChiefGandalf
2 Replies

4. Shell Programming and Scripting

Extract date / time

How do i display in the below format without the brackets using shell script. Tue Oct 1 13:32:40 2013 Please use CODE tags not only for all code segments, input samples, and output samples. (7 Replies)
Discussion started by: ghosh_tanmoy
7 Replies

5. IP Networking

ping can not recognize host but host command can

Hi, I have a weird problem. when ever I do ping command like for example ping unix.comI get the following message: # ping unix.com ping: unknown host unix.com but when I use host the computer is able to know the host. # host unix.com unix.com has address 81.17.242.186 unix.com mail is... (2 Replies)
Discussion started by: programAngel
2 Replies

6. UNIX for Dummies Questions & Answers

Host command

hi guys I have standalone Suse 10 SP3 that when I run # hostname webserver but when I run host command # host webserver ;; connection timed out; no servers could be reached I would like to get the IP when I issue host webserver x.x.x.x and the name when host x.x.x.x... (1 Reply)
Discussion started by: karlochacon
1 Replies

7. Shell Programming and Scripting

How to extract date with time from file

Hi, I have a file where there is a date and time field, the format for it is yyyy-mm-dd hours:mins:sec the position of date field may vary anywhere in the line and it might be different and it is specified along with the variable AppTimeStamp how do i extract date and time both from the... (5 Replies)
Discussion started by: prash_b
5 Replies

8. UNIX for Advanced & Expert Users

How to extract two words at the same time.

Hi, Can anyone please let me know, how to extract two lines at the same time. In specific,I have a file containing list of devices, such as router1 and switch2 below. I want to get all the lines which has "#" and all the lines which has "down" router1#sh ip int br Interface ... (6 Replies)
Discussion started by: Aejaz
6 Replies

9. UNIX for Dummies Questions & Answers

Extract TIME

Hey guys. How would I go about extracting the time from the system clock and inserting it into a variable? Any thoughts? (4 Replies)
Discussion started by: ndoggy020
4 Replies

10. UNIX for Advanced & Expert Users

using: ssh host command

I will remote a connection to a Unix server and launch a command on this server. This commnand requires me to set a view in Clearcase to see the vob element. How do I set a view an run a command with ssh with the syntax: ssh host command? (3 Replies)
Discussion started by: majo
3 Replies
Login or Register to Ask a Question