bash循环内的管道与循环后的管道

问题描述

我正在网上挑战。 lvl要求我们做一些蛮力。

那么,这两种解决方案之间有什么区别:

## Solution 1
for i in $(seq -w 9999); do 
  echo "password_prevIoUs_lvl $i" 
done | nc localhost 30002

## Solution 2
for i in $(seq -w 9999); do 
  echo "password_prevIoUs_lvl $i" | nc localhost 30002
done

这里唯一改变的是管道。

我尝试仅在Shell中执行命令。输出为:

bandit24@bandit:~$ echo "password_prevIoUs_lvl 2525" | nc localhost 30002
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line,separated by a space.
Wrong! Please enter the correct pincode. Try again.

Timeout. Exiting.

超时。

如果我使用其他密码直接在shell中重试,则输出为:

bandit24@bandit:~$ echo "password_prevIoUs_lvl 2525" | nc localhost 30002
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line,separated by a space.
Wrong! Please enter the correct pincode. Try again.
password_prevIoUs_lvl 2526
Timeout. Exiting.

只是超时,然后像以前一样结束,并且没有错误消息。

所以我不明白为什么在循环末尾使用编号为1的解决方案时,我会正确地得到此结果:

bandit24@bandit:/tmp$ ./b.sh
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line,separated by a space.
Wrong! Please enter the correct pincode. Try again.
Wrong! Please enter the correct pincode. Try again.
......
Wrong! Please enter the correct pincode. Try again.
Correct!
The password of user bandit25 is XXXXXXXXXXXXXXX

Exiting.

有人可以向我解释原因吗?

一个问题是:为什么在找到正确的销钉后它不会结束?

非常感谢您的帮助。

解决方法

感谢@Raman Sailopal的评论

In the first solution you are opening the connection only once and sending the echo statement 9999 times. In the second,you set up the connection 9999 times and then send the echo statement once for each connection.