Shell / Bash脚本telnet测试多播地址

时间:2013-08-15 19:56:47

标签: bash shell unix

我正在尝试创建一个shell / bash脚本来telnet测试到多播地址。我目前有以下内容:

telnet <ip_address> <port_number>

然而,这种情况仍然存在:

<server_name>% ./telnet_test.sh
Trying <ip_address>...
Connected to <ip_address>.
Escape character is '^]'.
//Hangs here for a few seconds...
Connection to <ip_address> closed by foreign host.

如何成功测试多播地址并立即返回,以便我不必等到外部主机关闭连接。

我需要在同一个脚本中测试多个地址。

编辑:

我应该提到我是一个银行网络,防火墙控制严密,我不能安装netcat。

此测试的目的只是为了查看我是否可以从当前主机连接到该路由。

2 个答案:

答案 0 :(得分:2)

请改用netcat -z - 扫描听力守护进程 -w 3 - 3秒后超时

#!/bin/bash

host_list="www.google.com www.stackoverflow.com"
port=80

for host in $host_list; do
    nc -z -w 3 $host $port > /dev/null 2>&1
    if [[ $? = 0 ]]; then
        printf "%s%s%s\n" " [$host:" "UP" "]"
    else
        printf "%s%s%s\n" " [$host:" "DOWN" "]"
    fi
done

答案 1 :(得分:0)

如果您安装了expect,则可以使用它来控制telnet

#!/usr/bin/except
spawn telnet <ip address> <port number>
set timeout 10
expect "Connected to"
send "exit\r"

检查man expectsome other article以获取更多信息。