Solved

IP address from a pseudo-terminal device

Posted on 2000-02-19
11
316 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 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