MOHAMED
MOHAMED

Reputation: 43528

How to know the source ip address used by nc to connect to the destination

I use nc in order to check the connectivity to my server:

echo -e -n "" | nc 192.168.15.200 8080

And I want to know the source ip address used by nc to connect to the server.

If it is not possible to extract the source ip address with nc command, Are there other alternative to retrieve the source ip ddress used in the connection to my server ?

Upvotes: 2

Views: 9024

Answers (1)

The following script retrieve the source IP address used by nc in the connection to your destination.

#!/bin/sh

dest=192.168.15.200 # You can put an ip address or a name address
port=8080

isIP=`echo "$dest"|grep -v -e [a-zA-Z]`

echo -e -n "" | nc $dest $port

if [ "_$isIP" != "_" ];then
    ip=`netstat -t -n|grep $dest:$port|sed 's/ \+/ /g'|cut -f4 -d " "|cut -f1 -d:`
else
    for addr in `nslookup $dest|grep -v \#|grep Address|cut -f2 -d:|sed 's/\ //g'`;do
        ip=`netstat -t -n|grep $addr:$port|sed 's/ \+/ /g'|cut -f4 -d " "|cut -f1 -d:`
        if [ "_$ip" != "_" ];then
            break
        fi
    done
fi
echo $ip

Upvotes: 1

Related Questions