0

I'm trying to set up a script that deploys out a repo file to a number of remote servers as part of a first time setup for our on-site patching mirror. When I run the script, it only runs the loop once and then exits. I'm not super experienced with bash, so I'm having some issues diagnosing why it's exhibiting this behavior.

Here's the code I've written.

while read client; do
    echo "Running configuration on $client"
    echo "-------------------------------------------------------------------"
    sshpass -p XXXXXXXXXXXXX ssh-copy-id root@$client
    ssh root@$client "mkdir /etc/yum.repos.d/repo-backup"
    ssh root@$client "mv /etc/yum.repos.d/* /etc/yum.repos.d/repo-backup/"
    scp local.repo root@$client:/etc/yum.repos.d/
    ssh root@$client "mv /etc/yum/pluginconf.d/rhnplugin.bkp"
    scp rhnplugin.conf root@$client:/etc/yum/pluginconf.d/
    ssh root@$client "yum clean all"
    ssh root@$client "rm -rf /var/cache/yum"
    ssh root@$client "yum repolist"
done < $clientlist

For the purposes of this, lets say that $clientlist is a file as follows:

foo.company.com
bar.company.com
testbench.company.com

All of these servers are accessible and resolving DNS. Each step of the script runs fine on foo.company.com, however once it runs "yum repolist", it just exits. I've run the following test code and it works just fine; so I have the loop's structure right; I don't understand why it's not looping in the real code but but is in the test code.

while read client; do
    echo $client
done < $clientlist
braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

0

ssh consumes stdin, and you're redirecting the file into the loop on stdin.

The simplest solution is to use a different file descriptor:

while read client <&3; do
    ...
done 3< $clientlist
glenn jackman
  • 238,783
  • 38
  • 220
  • 352