寻找下一个开放端口

发布于 2024-11-28 01:31:31 字数 135 浏览 1 评论 0原文

有没有什么办法,使用基本的 Unix 命令,找到下一个未使用的端口号,从端口 4444 开始向上?我通过 ssh(通过 openssh)进入 Windows XP 计算机,运行 Cygwin 工具并使用 bash shell。

谢谢,-戴夫

Is there any way, using basic Unix commands, to find the next unused port number, starting at port 4444 and going upwards? I'm ssh'ed (via openssh) into a Windows XP machine, running Cygwin tools and using a bash shell.

Thanks, - Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-12-05 01:31:31

试试这个:

for port in $(seq 4444 65000); do echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done

如果 telnet 以退出代码 1 结束,

seq 4444 65000 - port range for check
echo -ne "\035" - escape character to force close telnet session (^])

则意味着连接被拒绝:

$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
$ echo $?
1

否则我们认为连接成功,退出代码为 0。

编辑:
cygwin 特别:您需要安装包含 telnet 端口的附加软件包 inetutils 并使用脚本如下:

for port in $(seq 4444 65000); do echo -ne "\035" | /usr/bin/telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done

Try this:

for port in $(seq 4444 65000); do echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done

where

seq 4444 65000 - port range for check
echo -ne "\035" - escape character to force close telnet session (^])

if telnet finishes with exit code 1 that mean connection refused:

$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
$ echo $?
1

else we decided that connection was success with exit code 0.

EDIT:
Special for cygwin: You need to install additional package inetutils that is contain telnet port and use the script as follows:

for port in $(seq 4444 65000); do echo -ne "\035" | /usr/bin/telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done
不语却知心 2024-12-05 01:31:31

与上面相同,但是写成函数

function get_unused_port() {
  for port in $(seq 4444 65000);
  do
    echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1;
    [ $? -eq 1 ] && echo "$port" && break;
  done
}
FREE_PORT="$(get_unused_port)"
echo $FREE_PORT

Same as above, but written as a function

function get_unused_port() {
  for port in $(seq 4444 65000);
  do
    echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1;
    [ $? -eq 1 ] && echo "$port" && break;
  done
}
FREE_PORT="$(get_unused_port)"
echo $FREE_PORT
旧伤慢歌 2024-12-05 01:31:31

以下函数不依赖于 telnet/netcat,因为它会在本地端口范围内生成随机端口,并将其与正在运行的应用程序当前使用的端口列表进行比较。

应该适用于任何支持 proc 文件系统的 *nix。生成一个免费的临时端口供您的应用程序使用。

function EPHEMERAL_PORT(){
    while true; do 
        LISTENING_PORTS=$(cat /proc/net/tcp | awk 'NR >1 {print $2}' | awk -F':' '{print $2}');
        LISTENING_PORTS=$(for PORT in ${LISTENING_PORTS}; do echo $((16#${PORT})); done|sort -g);
        # echo "32768 60999" | read LPORT UPORT
        read LPORT UPORT < /proc/sys/net/ipv4/ip_local_port_range
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then
            echo $MPORT;
            break;
        fi
    done
}

显然 TCP 连接可以在 Linux 上用作文件描述符。以下函数使用该技术,并且应该比前一个函数更快。

function EPHYMERAL_PORT(){
    LPORT=32768;
    UPORT=60999;
    while true; do
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        (echo "" >/dev/tcp/127.0.0.1/${MPORT}) >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo $MPORT;
            return 0;
        fi
    done
}

这是一个跨平台函数,使用 osquery 获取监听端口列表。应该也可以在 Windows 上运行。

function EPHYMERAL_PORT(){
    while true; do 
        echo "32768 60999" | read LPORT UPORT
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        LISTENING_PORTS=$(osqueryi --header=false --list "select port from listening_ports order by port");
        if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then
            echo $MPORT;
            break;
        fi
    done
}

使用说明。将输出绑定到变量并在脚本中使用。在 Ubuntu 16.04 上测试

root@ubuntu:~> EPHYMERAL_PORT
59453
root@ubuntu:~> PORT=$(EPHYMERAL_PORT)

The following function doesn't depend on telnet/netcat as it generates a random port in the local port range and compares it with a list of ports currently used by running applications.

Should work on any *nix that supports proc filesystem. Generates a free ephemeral port to be used by your application.

function EPHEMERAL_PORT(){
    while true; do 
        LISTENING_PORTS=$(cat /proc/net/tcp | awk 'NR >1 {print $2}' | awk -F':' '{print $2}');
        LISTENING_PORTS=$(for PORT in ${LISTENING_PORTS}; do echo $((16#${PORT})); done|sort -g);
        # echo "32768 60999" | read LPORT UPORT
        read LPORT UPORT < /proc/sys/net/ipv4/ip_local_port_range
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then
            echo $MPORT;
            break;
        fi
    done
}

Apparently TCP connections can be used as file descriptors on linux. The following function uses that technique and should be faster than the previous one.

function EPHYMERAL_PORT(){
    LPORT=32768;
    UPORT=60999;
    while true; do
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        (echo "" >/dev/tcp/127.0.0.1/${MPORT}) >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo $MPORT;
            return 0;
        fi
    done
}

This is a cross platform function that uses osquery to get a list of listening ports. Should also work on Windows.

function EPHYMERAL_PORT(){
    while true; do 
        echo "32768 60999" | read LPORT UPORT
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        LISTENING_PORTS=$(osqueryi --header=false --list "select port from listening_ports order by port");
        if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then
            echo $MPORT;
            break;
        fi
    done
}

Usage instructions. Bind the output to a variable and use in scripts. Tested on Ubuntu 16.04

root@ubuntu:~> EPHYMERAL_PORT
59453
root@ubuntu:~> PORT=$(EPHYMERAL_PORT)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文