[英]netcat inside a while read loop returning immediately
I am making a menu for myself, because sometimes I need to search (Or NMAP which port). 我正在为自己制作菜单,因为有时我需要搜索(或NMAP哪个端口)。 I want to do the same as running the command in the command line.
我想做与在命令行中运行命令相同的操作。
Here is a piece of my code: 这是我的一段代码:
nmap $1 | grep open | while read line; do
serviceport=$(echo $line | cut -d' ' -f1 | cut -d'/' -f1);
if [ $i -eq $choice ]; then
echo "Running command: netcat $1 $serviceport";
netcat $1 $serviceport;
fi;
i=$(($i+1));
done;
It is closing immediately after it scanned everything with nmap. 它使用nmap扫描所有内容后立即关闭。
Don't use FD 0 (stdin) for both your read loop and netcat. 请勿将FD 0(stdin)用于读取循环和netcat。 If you don't distinguish these streams,
netcat
can consume content emitted by the nmap | grep
如果不区分这些流,
netcat
可以使用nmap | grep
发出的内容nmap | grep
nmap | grep
pipeline rather than leaving that content to be read by read
. nmap | grep
管道,而不是让该内容由read
。
This has a few undesirable effects: Further parts of the while/read loop don't get executed, and netcat
sees a closed stdin stream and exits when the pipeline's contents are consumed (so you don't get interactive control of netcat, if that's what you're trying to accomplish). 这会产生一些不良影响:while / read循环的其他部分不会执行,并且
netcat
看到封闭的stdin流,并且在消耗管道内容时会退出(因此,您不会获得对netcat的交互式控制,您要完成的任务)。 An easy way to work around this issue is to feed the output of your nmap pipeline in on a non-default file descriptor; 解决此问题的一种简单方法是将nmap管道的输出输入到非默认文件描述符中。 below, I'm using FD 3.
下面,我正在使用FD 3。
There's a lot wrong with this code beyond the scope of the question, so please don't consider the parts I've copied-and-pasted an endorsement, but: 这段代码有很多错误,超出了问题的范围,因此,请不要考虑我复制粘贴的部分认可,但是:
while read -r -u 3 line; do
serviceport=${line%% *}; serviceport=${serviceport##/*}
if [ "$i" -eq "$choice" ]; then
echo "Running command: netcat $1 $serviceport"
netcat "$1" "$serviceport"
fi
done 3< <(nmap "$1" | grep open)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.