linux - 如何知道nc使用的源ip地址连接到目的地

标签 linux sockets netcat

我使用 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/

相关文章:

linux - 在 Linux VM(虚拟机)上,以太网连接在重新启动时断开

linux - nmap 说端口已关闭,而 nesstat 说它正在监听

linux - 如何在 Linux 中创建 Jenkins SSH key 以用于 Github?

linux - 在不使用双引号的情况下搜索包含当前日期的日志文件

android - gstreamer可以播放使用下面这个客户端代码发送的媒体内容吗?

c++ - tcp 连接上的 recv() 问题

tcp - TCP 流中什么时候发生管道破裂?

bash - 将十六进制转换为二进制并通过网络发送

linux - 用于检查远程服务器进程的 Perl 脚本

java - Socket.io 无需加入即可收听房间