bash Shell 脚本 - telnet 多个主机:端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29392568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:42:28  来源:igfitidea点击:

Shell Script - telnet multiple hosts:ports

bashshelltestingshtelnet

提问by TPatriot

I'm not an expert in shell script by any means. I got the structure idea from another post (Bash script telnet to test multiple addresses and ports) My need is to verify LAN connections between specific hosts and ports via telnet.

无论如何,我不是 shell 脚本方面的专家。我从另一篇文章(用于测试多个地址和端口的 Bash 脚本 telnet )中获得了结构思想,我需要通过 telnet 验证特定主机和端口之间的 LAN 连接。

The reason for using telnet is the fact that both the LAN and machines are heavily secure and I don't have access to netcat, nmap or /dev/tcp. I'm also no where near comfortable with Python or Pearl to try that route... ( I know silly me, I'll get there though :P ).

使用 telnet 的原因是 LAN 和机器都非常安全,我无法访问 netcat、nmap 或 /dev/tcp。我也不太习惯使用 Python 或 Pearl 来尝试这条路线......(我知道我很傻,不过我会到达那里 :P )。

The following code works, however for reasons beyond my understanding the while loop iterates only once and no more... :( .

下面的代码有效,但是由于我无法理解的原因,while 循环只迭代一次,不再迭代...... :(。

Note: it is important for me to know if the connection failed due to timeout or was refused (port is closed at the endpoint).

注意:了解连接是否因超时或被拒绝(端口在端点关闭)而失败对我来说很重要。

Can anyone help me in 1) fixing it and 2) understanding why?

任何人都可以帮助我 1) 修复它和 2) 理解为什么?

FYI: For anyone else that might have a similar need here's the fully operational updated code for the script. In this case connection refused is being handled as a success (testing firewall rules) which can be changed to failed depending on necessities.

仅供参考:对于可能有类似需求的其他任何人,这里是脚本的完全可操作的更新代码。在这种情况下,拒绝连接被视为成功(测试防火墙规则),可以根据需要更改为失败。

    #!/bin/bash
    path=`pwd`;
    touch $path/test_telnet.out || exit;
    touch $path/success.log || exit;
    touch $path/failed.log || exit;
    echo "10.192.168.1 1200
    10.10.10.2 80
    10.220.2.8 6090
    10.220.2.9 6090" | ( while read host port; do
        telnet $host $port </dev/null > $path/test_telnet.out 2>&1 & sleep 1; kill $!;
        if grep Connected $path/test_telnet.out >/dev/null;
            then
                echo @ $(date +"%b %d %H:%M %Y") $host:$port [ OPEN ] | tee -a $path/success_log.txt;
            elif grep refused $path/telnet_test.txt >/dev/null; then
                echo @ $(date +"%b %d %H:%M %Y") $host:$port [ REFUSED ] | tee -a $path/success_log.txt;
            else
                echo @ $(date +"%b %d %H:%M %Y") $host:$port [ TIMEOUT ] | tee -a $path/failed_log.txt;
        fi;
    cp /dev/null $path/test_telnet.out;
    done
    ) 2>/dev/null #avoid bash messages

回答by glenn Hymanman

As Etan commented, telnet is eating the rest of your input. The fix is to redirect the input for telnet.

正如 Etan 评论的那样,telnet 正在吞噬您的其余输入。解决方法是重定向 telnet 的输入。

Change this:

改变这个:

telnet $host $port > ~/test_con/telnet_test.txt

to this:

对此:

telnet $host $port </dev/null > ~/test_con/telnet_test.txt

回答by Anand Varkey Philips

I have added a shell script file and just run this. This script get servers and ports from below list.

我添加了一个 shell 脚本文件并运行它。此脚本从下面的列表中获取服务器和端口。

for i in {10.21.xxx.yyy,10.21.xxx.yyy,10.23.xxx.yyy};
do
        for j in {5501,5502,5503,5504,7701,7702,7703,7704,5551,5552,5553,7771,7772,7773};
        do
                (echo > /dev/tcp/${i}/${j}) > /dev/null 2>&1 && echo "${i}:${j} :: it's getting connected" || echo "${i}:${j} :: it's not connecting"
        done
done