Solved

IP address from a pseudo-terminal device

Posted on 2000-02-19
11
314 Views
Last Modified: 2013-12-06
How can i find the IP address or the host name of people telneting to my UNIX machine. This is to be used in a shell script and should be clean. I.e i would prefer not to us finger or last.
I wish to use this in an application that will ftp results back to people who telnet into the UNIX application and who are running a ftpd on their PC's.
0
Comment
Question by:richardgraham
  • 4
  • 4
  • 3
11 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2540363
Type in the command "who".

If should give you all of the users logged in.  Users telnet'ed in to your system will have the hostname at the end of their line... usually in parenthesis...

You can parse this in your shell script.


Cheers!
0
 
LVL 3

Expert Comment

by:klover
ID: 2541214
netstat -n|grep :23

will give you a list of all IPs currently attached to your Unix machine via telnet.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2541349
"who" gives the username and hostname...

"netstat -n|grep :23" gives the username and IP address


Cheers!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2541353
by the way, "who" also gives the pseudo terminal name, "netstat -n|grep :23" doesnt...


Cheers!
0
 
LVL 3

Expert Comment

by:klover
ID: 2541723
On my SCO OpenServer 5.0.5 box a "who" does not show the hostname or IP.  Checked the man, "w -x" is required to get the IP or hostname of the user.  

What flavor of Unix are you running?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:klover
ID: 2541771
My shell scripting is kind of weak but you could put something like this in the .profile or .login of the user ...

#set environment variable
MYTTY=sed<`tty`;export MYTTY

#set IP of user
IPADDR=sed <who -x|grep $MYTTY

#whatever other commands they run

#ftp them their files when done
ftp $IPADDR

Both sed commands would need some more work.  In the first one tty returns /dev/tty#, so you'd need to use sed (or awk?) to strip off the leading /dev/.  Then the who -x is going to return a whole line of info, so you'd need to strip out the IP somehow.
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2548353
Try IPADDR=awk '{print $n}' < `who am i` in their .profile or .login

where n is the field number of the ip address (or hostname, if the address is in /etc/hosts) returned by `who am i`

Depending on your Unix, you may have to add options (e.g. -h) to the `who` command to get it to display the IP address/hostname
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2559589
Oops - wrong way round; Should have said:

IPADDR=`who am i | awk '{print $n}'`

where n is the field number of the ip address (or hostname, if the address is in /etc/hosts) returned by `who am i`



0
 
LVL 14

Expert Comment

by:mcrider
ID: 2560426
tfewster, Please re-read the question again. your solution *does not* give the IP address of OTHER people connecting to the system...

Suggest you remove your answer...
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2561068
mcrider, as I understand it richardgraham's requirement is to get the hostname/address of a particular user session, not just to see who's on the system, possibly with multiple logins. However, I've withdrawn my answer in case I've misunderstood.

richardgraham: In THEIR .profile/.login
IPADDR=`who am i | awk '{print $n}'|sed s/(//|sed s/)//` will return the "address" for that session. Alternatively, you could capture this info. when they run the application to save having to modify everyone's login scripts.

The sed statements i've added deal with the case that "who" returns a hostname (usually in parentheses). The parentheses in the sed statements may need to be "escaped" with \ to prevent them being interpreted by the shell.

If the expected usage is: User logs in, kicks off a query & then logs out, with results being ftp'd to them later, you will have to save their address & tie it to the query results file. Be warned though that automating ftp has it's own problems.





0
 
LVL 20

Accepted Solution

by:
tfewster earned 75 total points
ID: 2576255
See previous comments:
 
Solaris & AIX:  use `who -m |awk '{print $6}' | sed s/\(//|sed s/\)//`

HP-UX:use `who -m -u | awk '{print $8}'`

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parsing a file using ksh 10 59
How to check files recursively from multiple directories and get latest file in bash script 6 94
unix in java example 9 55
aix unix tar error 3 44
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

863 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

24 Experts available now in Live!

Get 1:1 Help Now