Problems with using SSH commands in a while loop under the shell

Source: Internet
Author: User
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

  1. Use the-n parameter of ssh: use/dev/null to ssh input, prevent SSH from reading the local standard input content.
  2. SSH xxxx </dev/null redirect the input of the shell command

Problems with using SSH commands in a while loop under the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.