1 Description of the phenomenon
The most recent batch execution command using SSH (with key trust already done), the script reads the list of hosts in the configuration file (the content is one host IP address per line), and then executes, but every time just executes the first one, it exits the loop.
2 Troubleshooting Ideas
Because the script is relatively simple, just read the host, and then SSH, so the problem should appear on SSH
3 Identifying the problem
Why does SSH command cause the shell to quit, and finally search the Internet to find the answer.
Each time the SSH command executes, it reads all the contents of the standard input.
For a while loop, when used as follows:
While read line do echo $Linedone < $filename
Here, the redirect is used to enter the contents of the file into the while command, while the while command reads a row of data from the input each time it is read.
The problem is here, if the SSH command is called in the while loop, then SSH will read all the data in the current input, that is, the data redirected to the while command is read by the SSH command, so that the next loop, read the content is empty, causing the loop to end prematurely.
4 Processing Results
Two ways of solving this problem
- Use the-n parameter of ssh: use/dev/null to ssh input, prevent SSH from reading the local standard input content.
- SSH xxxx </dev/null redirect the input of the shell command
Problems with using SSH commands in a while loop under the shell