2012-05-17 22 views
1

我用NC以檢查連接到我的服務器:如何知道NC正在使用的源IP地址連接到目標

echo -e -n "" | nc 192.168.15.200 8080 

而且我想知道NC正在使用的源IP地址連接到服務器。

如果無法使用nc命令提取源IP地址,還有其他替代方法可以檢索連接中使用的源IP地址嗎?

回答

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 
+0

很好的答案,謝謝 – MOHAMED

+0

您還可以指定使用-s標誌的源地址 – GeoSword

相關問題