Operating System - HP-UX
1755142 Members
3404 Online
108830 Solutions
New Discussion юеВ

Issue obtaining the users IP

 
SOLVED
Go to solution
Jay LaBonte
Advisor

Issue obtaining the users IP

I have a situation in a script called from a web page that uses the following statement:

/usr/bin/who -u | /usr/bin/grep `/usr/bin/who -m -R | /usr/bin/awk '{ print $2 }'`" " | /usr/bin/awk '{ print $8 }' | /usr/bin/nsl
ookup | /usr/bin/grep Address | /usr/bin/awk '{ print $2 }'

This script works great for anyone logged in on a normal terminal session. However, when called by a script from a web page via a cgi script I get the following response:

"process not attached to terminal"

I know it bombing at the "who -m". Problem is I can not figure out how to get around this issue.

Does any one have any idea how to obtain the IP address from a connection where the process is not attached to a terminal session?

11 REPLIES 11
Simon Hargrave
Honored Contributor

Re: Issue obtaining the users IP

If it's in a cgi script, why not just query the environment variable REMOTE_ADDR, which is passed as standard via CGI?
Jay LaBonte
Advisor

Re: Issue obtaining the users IP

Because the http server in use at this customer site is a home grown server, and it does not have REMOTE_ADDR available. (Beleive me I have asked them to switch to another http server like apache, but that have to must custom code to convert, so Im stuck with the situation)

So now that that is out of the way, any real suggestions?
Simon Hargrave
Honored Contributor

Re: Issue obtaining the users IP

Well in that case you probably don't have an option. The connection to the "shell" will not be from the connecting user, rather it will be from the httpd itself. That is, if you could get the IP address of "whatever ran the who command", this would always be the same, ie the IP address of the server the httpd daemon is running. The only thing that knows the connecting address is the http daemon itself. It it doesn't pass the information that it should, you can't extract it.
Jay LaBonte
Advisor

Re: Issue obtaining the users IP

Acually there is no httpd is this situation. The way they have it set up is throught inetd.conf and services.

The services entry specifies: UniHTTP 8080

Then in the inetd.conf file they have the following entry:

UniHTTP stream tcp nowait root /opt/acc/ud/bin/http.sh http.sh

http.sh then calls a 'C' routine that lanches an application within their database environment.

So the actual deamon performing the call is inetd. Is there anyway of extracting the IP from inetd or getting inetd to append the IP to the shell call as a parameter?

Muthukumar_5
Honored Contributor

Re: Issue obtaining the users IP

Your inetd script as,

UniHTTP stream tcp nowait root /opt/acc/ud/bin/http.sh http.sh

You can send parameter to that script as,

UniHTTP stream tcp nowait root /opt/acc/ud/bin/http.sh http.sh `shell action to get ip there and send it there`


And your try as,
/usr/bin/who -u | /usr/bin/grep `/usr/bin/who -m -R | /usr/bin/awk '{ print $2 }'`" " | /usr/bin/awk '{ print $8 }' | /usr/bin/nsl
ookup | /usr/bin/grep Address | /usr/bin/awk '{ print $2 }'

seems to be very strange there.

you can use getip command here.

getip

So you can get the remote ip-address as,

getip `who -mR | awk '{ print $6 }' | tr -d '()'`

It will give the IP-Address there easily.

HTH.

Easy to suggest when don't know about the problem!
Jay LaBonte
Advisor

Re: Issue obtaining the users IP

Well i like the script for obtaining the ip, however it does not perform the command inside the `` is simply hands the command over the shell script without executing.
Muthukumar_5
Honored Contributor

Re: Issue obtaining the users IP

Is getip `who -mR | awk '{ print $6 }' | tr -d '()'` not working there?

can you verify step by step as,

1. IP=$(who -mR | awk '{ print $6 }' | tr -d '()')
2. getip $IP

Try to execute with full path of getip , who there. You can that with which getip / which who there. Post your problem with your try again.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Bill Hassell
Honored Contributor

Re: Issue obtaining the users IP

The simplest way to find the address from who is to NEVER count fields or characters. The IP address is always the last field:

IP=$(who -mu | awk '{print $NF}'

The answer is that your process is not attached to a terminal. Just like cron or at, inetd has no terminal and therefore an IP address is meaningless. You'll have to extract the adress for the web page from your web server as who does not understand web pages (no Unix login).


Bill Hassell, sysadmin
Jay LaBonte
Advisor

Re: Issue obtaining the users IP

The problem isn't that the getip statement is breaking down at some point. The problem is, inetd is not processing the statement between the tics, `who -mR | awk '{ print $6 }' | tr -d '()'`.

inetd is passing the statement to the target program as a parameter, without evaluating the statement first.

As a result the target script will see $x value as follows:
$1 = `who
$2 = -mR
$3 = |
$4 = awk
etc...