Jaxnoth has asked for the wisdom of the Perl Monks concerning the following question:

This may seem like an odd piece of info to need. However, is there a way to get the ip address of whomever is telnetted in at the current moment.

Essentially, I need to modify everyone's login script to set an environmental variable to their ip address. I am hoping perl can do this. Otherwise, I have to kludge together who and a couple of netstats to do it all. There has to be a better way.

Thanks.

Replies are listed 'Best First'.
Re: telnet ip address
by traveler (Parson) on Jul 23, 2002 at 20:39 UTC
    If you are using *nix, robobunny's solution looks easy enough and you can use perl instead of the pipeline to process the output of 'who'. OTOH, if you want a slightly more perlish solution, you can use Sys::Utmp to look up the login record. You can find out what the login device is with the shell's tty command.

    If you want to support *nix, Win*, etc, I'd possibly put a shim on port 23 (telnet) so it logged the connection somewhere before starting the real telnet daemon.

    Win32 may have an environment variable you could use instead, but that probably depends on what telnet server you are using. I suggest CLIENTNAME is a place to start. HTH, --traveler

Re: telnet ip address
by robobunny (Friar) on Jul 23, 2002 at 18:48 UTC
    'who' is your best bet, although the syntax you want is going to depend on your OS to some degree. i don't know what you'd be using netstat for. here's a chunk of code i use to get IP's under solaris and tru64/OSF1
    OS=`uname` if [ "$OS" = "SunOS" ]; then IP=`who am i | cut -f 2 -d \( | cut -f 1 -d \)` elif [ "$OS" = "OSF1"]; then IP=`who -M am i | cut -f 2 -d \( | cut -f 1 -d \)` fi
Re: telnet ip address
by kschwab (Vicar) on Jul 24, 2002 at 01:42 UTC
    This may not be feasible in your situation, but if you migrate from telnet to openssh, you'll get an easily parsable SSH_CLIENT environment variable, and a few other benefits as well.
    $ env | grep SSH SSH_TTY=/dev/pts/44 SSH_CLIENT=172.19.69.68 1850 22
Re: telnet ip address
by Rex(Wrecks) (Curate) on Jul 23, 2002 at 19:12 UTC
    Hmm, your question seems to require a list of the IP's. You can do this fairly easily on the server side with (whatever your OS's equivalent) the FreeBSD netstat -an command. Parsing the output for IP-Octet.IP-Octet.IP-Octet.IP-Octet.23 (23 being the telnet port) should not be to hard. There are even a couple CPAN modules that will help with easy IP Addr parsing.

    Doing it this way is going to be much easier than a client side solution, IMHO.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!