【发布时间】:2014-09-20 03:48:35
【问题描述】:
我正在尝试编写一个 shell 脚本,它以 IP 地址和端口号作为输入并输出端口是否在主机上打开。我的 shell 脚本看起来像这样
#!/bin/bash
name=$(echo exit | telnet $1 $2 | grep "Connected")
if [ "$name" == "" ]
then
echo "Port $2 is not open on $1"
else
echo "Port $2 is open on $1"
fi
它工作正常,但我的输出包含 2 行,如下所示:
[root@ip-172-31-8-36 Scripts]# ./test.sh 172.31.35.246 7199
Connection closed by foreign host.
Port 7199 is open on 172.31.35.246
或
[root@ip-172-31-8-36 Scripts]# ./test.sh 172.31.35.246 7200
telnet: connect to address 172.31.35.246: Connection refused
Port 7200 is not open on 172.31.35.246
我想在这两种情况下抑制输出的第一行。 知道怎么做吗?
【问题讨论】:
-
为什么不直接使用
nmap实用程序? -
@cdhowie 我们公司的基础设施团队不会在系统上安装 nmap,所以我不得不使用 telnet