Question

On a Solaris 10 host there is an inetd service configured to start a bash script when it it gets an incoming TCP connection to a pre-configured port/service. Is there a way to find the IP address of the remote client in the invoked bash script?

If I was using the GNU version of inetd I would use the --environment command line flag. But I am using the default Solaris version of inetd/inetadm, which does not seem to support this flag. Is there a Solaris equivalent of this setting?

I also assume that getpeername(2) invoked on the fd of 0 (stdin) or 1 (stdout) would have returned the desired information but I am running a bash script and I don't seem to find a way to invoke an equivalent of getpeername(2) from bash.

Is my only option to invoke a C-wrapper that would do getpeername(2), store it in an environment variable (or a command-line argument), and invoke the main bash script?

Thank you!

Was it helpful?

Solution

You can get them by parsing pfiles output, something like:

pfiles $$ | grep peername | head -1 | nawk '{print $3}'

Edit:

Here is a lighter way in reply to Nemo's right comment about the number of processes launched:

pfiles $$ | nawk '/peername/ {print $3;exit}'

OTHER TIPS

You can invoke getpeername from a Perl one-liner:

perl -le 'use Socket; ($port,$addr) = sockaddr_in(getpeername(STDIN)); print inet_ntoa($addr);'

Wrap in backticks or whatever to run from a shell script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top