Linux uses SSH to the remote and uses the while pit

Source: Internet
Author: User
Tags stdin

If you want to use SSH bulk login to the operation of other systems, we will use a circular way to deal with, then there is a huge pit, you have to be careful.

Now you want to use a script to get the remote server side/root the following file:

1 # !/bin/bash 2 3 ' IP . txt '|  while Read Line;do 4         echo $line5         access= '    ssh  '  6 Done

The Result: The script only detects the first IP and jumps directly to the loop . 

Problem Analysis:

While using the redirect mechanism, the information in the Ip.txt file has been read into and redirected to the entire while statement, so when we call the Read statement again in the while loop, the next record is read. The problem here is that the SSH statement just reads everything in the input. To prevent SSH from reading all things add a </dev/null, redirect the input of SSH into the input.

resolution Policy:

1. Use a For loop to represent while, because for not one time the file content cache is fetched, the code snippet is modified as follows:

1  for inch ' cat ip.txt '; 2         Echo $ip3         access= ' ssh $ip "  '4         Echo $access5 done

2, if you persist in using the while loop, you need to add the-n parameter to SSH, why the addition of the-n parameter can also solve the problem? Read the description of the-n parameter through man SSH

Redirects stdin from/dev/null (actually, prevents reading from stdin)

1 # !/bin/bash 2 3 ' Ip.txt '|  while Read Line;do 4         echo $line5         "  "6 Done

Linux uses SSH to the remote and uses the while pit

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.