如何知道nc用于连接目标的源IP地址

时间:2012-05-17 17:53:10

标签: linux sockets netcat

我使用nc来检查与服务器的连接:

echo -e -n "" | nc 192.168.15.200 8080

我想知道nc用来连接服务器的源IP地址。

如果无法使用nc命令提取源IP地址,是否还有其他方法可以检索与我的服务器连接中使用的源IP地址?

1 个答案:

答案 0 :(得分:1)

以下脚本检索nc在与目标的连接中使用的源IP地址。

#!/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