Bash 脚本 telnet 测试多个地址和端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23421917/
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
Bash script telnet to test multiple addresses and ports
提问by Kyle
I am required to test at least 130 ip addresses and ports. I am hoping to write a bash script such that it reads the ip address and ports from an input file.
我需要测试至少 130 个 IP 地址和端口。我希望编写一个 bash 脚本,以便它从输入文件中读取 IP 地址和端口。
I have the following
我有以下
while read line
do
telnet $line >>
done <
This is a crappy code as it cannot determine whether its connected or failed, and I have to rely on its auto escape character to disconnect from a connection.
这是一个糟糕的代码,因为它无法确定它是连接还是失败,我必须依靠它的自动转义字符来断开连接。
How can I improvise this such that it updates $2 with the status quickly? I am working on Redhat and do not have netcat or expect installed..
我怎样才能即兴创作,使其快速更新 $2 的状态?我在 Redhat 上工作,但没有安装 netcat 或 expect ..
回答by ymonad
As other stackoverflower's said, I would recommend using nmap
or netcat
if avilable.
However, if you cannot use those software, you can use bash's builtin /dev/tcp/<host>/<port>
instead.
正如其他stackoverflower所说,我建议使用nmap
或netcat
如果可用。但是,如果您不能使用这些软件,则可以改用 bash 的内置程序/dev/tcp/<host>/<port>
。
http://www.gnu.org/software/bash/manual/bashref.html#Redirections
http://www.gnu.org/software/bash/manual/bashref.html#Redirections
I could'nt figure out which version of bash you are using, but /dev/tcp/...
seems to implemented since some old bash.
我无法弄清楚您使用的是哪个版本的 bash,但/dev/tcp/...
似乎是从一些旧的 bash 开始实现的。
#!/bin/bash
echo "scanme.nmap.org 21
scanme.nmap.org 22
scanme.nmap.org 23
scanme.nmap.org 79
scanme.nmap.org 80
scanme.nmap.org 81" | \
while read host port; do
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo $host $port is open
else
echo $host $port is closed
fi
done
This produces
这产生
scanme.nmap.org 21 is closed
scanme.nmap.org 22 is open
scanme.nmap.org 23 is closed
scanme.nmap.org 79 is closed
scanme.nmap.org 80 is open
scanme.nmap.org 81 is closed
UPDATED: The following can do timeout. Although it may seem little tricky, idea is just to kill the child process after some timeout.
更新:以下可以做超时。虽然看起来有点棘手,但想法只是在超时后杀死子进程。
Bash script that kills a child process after a given timeout
#!/bin/bash
echo "scanme.nmap.org 80
scanme.nmap.org 81
192.168.0.100 1" | (
TCP_TIMEOUT=3
while read host port; do
(CURPID=$BASHPID;
(sleep $TCP_TIMEOUT;kill $CURPID) &
exec 3<> /dev/tcp/$host/$port
) 2>/dev/null
case $? in
0)
echo $host $port is open;;
1)
echo $host $port is closed;;
143) # killed by SIGTERM
echo $host $port timeouted;;
esac
done
) 2>/dev/null # avoid bash message "Terminated ..."
this produces
这产生
scanme.nmap.org 80 is open
scanme.nmap.org 81 is closed
192.168.0.100 1 timeouted
since 192.168.100 does not exist in my local network.
因为 192.168.100 在我的本地网络中不存在。