0

This is my script

#!/bin/bash
exec < filelist.txt
    while read updatedfile oldfile; do
    #   echo updatedfile = "$updatedfile" #use for troubleshooting
    #   echo oldfile = "$oldfile" #use for troubleshooting
               if [[ ! $updatedfile =~ [^[:space:]] ]] ; then  #empty  line exception
                continue # empty line exception
               fi
               if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
                continue # empty line exception
               fi 
            echo Comparing $updatedfile with $oldfile
            if diff "$updatedfile" "$oldfile" >/dev/null ; then
                echo The files compared are the same. No changes were made.
            else
                echo The files compared are different.
                cp -f -v $oldfile /infanass/dev/admin/backup/`uname -n`_${oldfile##*/}_$(date +%F-%T)
                cp -f -v $updatedfile $oldfile 
            fi          
    done
#go through rest of servers from list
while read server <&3; do   #read server names into the while loop    
serverName=$(uname -n)
 if [[ ! $server =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
 echo server on list = "$server"
 echo server signed on = "$serverName"
 if [ $serverName == $server ] ; then #makes sure a server doesnt try to ssh to itself
    continue
 fi
    echo "Connecting to - $server"
    ssh "$server"  #SSH login
    exec < filelist.txt
    while read updatedfile oldfile; do
    #   echo updatedfile = $updatedfile #use for troubleshooting
    #   echo oldfile = $oldfile   #use for troubleshooting
               if [[ ! $updatedfile =~ [^[:space:]] ]] ; then  #empty line exception
                continue # empty line exception
               fi
               if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
                continue # empty line exception
               fi 
            echo Comparing $updatedfile with $oldfile
            if diff "$updatedfile" "$oldfile" >/dev/null ; then
                echo The files compared are the same. No changes were made.
            else
                echo The files compared are different.
                cp -f -v $oldfile /infanass/dev/admin/backup/`uname -n`_${oldfile##*/}_$(date +%F-%T)
                cp -f -v $updatedfile $oldfile 
            fi          
    done
done 3</infanass/dev/admin/servers.txt

I get the error when I try to ssh to my list of servers.

Pseudo-terminal will not be allocated because stdin is not a terminal.

Then my script doesn't correctly ssh and compare the files from the newly logged in server.. Does anybody know why this is?

1

Are you expecting that all the commands after ssh "$server" get run inside the ssh? That's not how it works. ssh with a hostname and no other arguments starts an interactive session. After it exits, the script continues with the next command (exec < filelist.txt). That's not a remote command inside the ssh; the ssh is done and gone by the time the command after it is reached. That's just normal sequential execution of the containing script.

Interactive ssh sessions with redirected stdin are somewhat unusual. That's why you get a warning. (To eliminate the warning you can use -t or -T)

If you want to pass a big script over an ssh connection and run it remotely, you can do it with a here document, like this:

ssh "$server" sh <<EOF
your big script here...
EOF

Be sure to carefully consider which variables should be expanded by the local script and which should be expanded during execution of the remote script. An unprotected $ in the heredoc will be expanded locally. To protect it so the remote shell sees a $, use \$. If you want all of them protected, you can change <<EOF to <<'EOF'.

  • Doesn't seem to be working for me – mkrouse Jul 16 '13 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.