我使用 nc 来检查与我的服务器的连接:
echo -e -n "" | nc 192.168.15.200 8080
而且我想知道nc用来连接服务器的源ip地址。
如果无法使用 nc 命令提取源 IP 地址,是否有其他替代方法来检索连接到我的服务器时使用的源 IP 地址?
最佳答案
以下脚本检索 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
关于linux - 如何知道nc使用的源ip地址连接到目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10640896/