Solved

IP address from a pseudo-terminal device

Posted on 2000-02-19
11
310 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
Comment Utility
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
Comment Utility
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
Comment Utility
"who" gives the username and hostname...

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


Cheers!
0
 
LVL 14

Expert Comment

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


Cheers!
0
 
LVL 3

Expert Comment

by:klover
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:klover
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 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

16 Experts available now in Live!

Get 1:1 Help Now