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

Dear Monks I have search the Q&A of networking but have not yet found an answer to my problem. We have a script which are building objects into a database, the problem is that it can be very nasty if you use it in the wrong way and the autitors of the project want to have a logging function added to it. They want to know the ip adress of the client opening a session on Unix machine which is executing the script. So how do I find out my pts ip number on unix?? Cheers Pär
  • Comment on Adress of client opening a Unix session

Replies are listed 'Best First'.
Re: Adress of client opening a Unix session
by Fletch (Bishop) on Sep 09, 2004 at 12:33 UTC

    Some things like ssh will set an environment variable, but since the user's free to muck with that before running your program it's not trustworthy. Slightly more reliable would be reading the remote host from /etc/utmp, but that's going to be somewhat platform dependent (see Sys::Utmp or User::Utmp) and if someone can manipulate the DNS server you're using it's likewise untrustworthy.

    A better solution would be to mandate seperate system logins for each user and use those to determine who's running your program. If someone can circumvent that they more than likely could alter your log anyhow.

      Yes I do agree with you, that the best thing would be to have different users, but that is not really possible because of the arcitecture. This "build" script is only meant for administrators of the system, but now during implementation other people need to use it and therefor we must log it. I'll have a look on the Sys::Utmp and the User::Utmp
      Thanks

        Ah, then you probably can use sudo and let it handle the logging. Just create a dedicated build user who's the owner of everything in question. Put the people who need to be able to run the build process in a group, and then configure sudo to allow that group to run whatever command / script is necessary to kick things off (and the original user who ran sudo will be logged to wherever syslog is pointing).

Re: Adress of client opening a Unix session
by diotalevi (Canon) on Sep 09, 2004 at 12:28 UTC

    It depends. You didn't provide enough information to help us help you.

    How are you connecting? ssh sets the environment variable $ENV{ 'SSH_CONNECTION' } for me. Perhaps you have some similar information. Other connections may have this sort of information elsewhere.

      We have putty installed and the users cannot install new programs on the client, so we connect via ssh or telnet. cheers Kurre
        Didn't you read what I just said? I already mentioned that I see the environment variable SSH_CONNECTION with the remote ip address in it. If you noticed that you have this, then use it. If you don't have it, it's probably the fault of your SSH server or maybe a configuration script on your server deletes it.
Re: Adress of client opening a Unix session
by Anonymous Monk on Sep 09, 2004 at 12:49 UTC
    How's that script executed? Is it listening to a port (in which case, the socket will provide you with the information)? Is it run from a remote shell? Is it executed by means of UUCP? Perhaps it's a CGI program? Or are you using SOAP? Is the script is mailed, and you're using a fancy .forward file? Is your .finger a named pipe?
      It is executed via a shell, by hand, by an authirized user,
      cheers
        Then 'finger' or 'who' should know. Or you'll have to parse /var/log/wutmpx? yourself.
        finger | grep `ps -o tty -p $$ | tail +2`" " | cut -b 56- | tr -d '()'
        Adjust to fit your implementation of your tools.