简体   繁体   中英

Solaris10: How to get the remote end IP address in inted-started bash script?

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!

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.

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}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM