0

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!

evolvah
  • 625
  • 4
  • 15

2 Answers2

2

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.

Nemo
  • 70,042
  • 10
  • 116
  • 153
  • Thank you for the answer! It would work but it looks a little heavier than the `pfiles $$` approach offered by jlliagre. Thanks again! – evolvah Jul 16 '12 at 21:08
  • I would not be so sure about "heavier". My answer is one extra process instead of four... Anyway, glad you have your answer :-) – Nemo Jul 16 '12 at 21:33
1

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}'
jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • Thank you for the quick and comprehensive answer! I took is a little step further by tightening up the grep pattern and using `cut` instead of `nawk`. – evolvah Jul 16 '12 at 21:06