1752786 Members
5577 Online
108789 Solutions
New Discussion

Re: scp by using script

 
tempsample
Frequent Advisor

scp by using script

for i in hostname

do

scp -r target files $i:/home/testuser

done

exit

 

 

 

since i am going to perform scp for 150 servers,i need to redirect logs to a file.

 

i need to check if target files is  copied to target host.

 

 

 

 

 

2 REPLIES 2
Matti_Kurkela
Honored Contributor

Re: scp by using script

> since i am going to perform scp for 150 servers,i need to redirect logs to a file.

 

As with most other commands, you can do it with the >, >>, 2> and/or 2>>  redirection operators.

The only trick is that you probably should use >>, so the logs of the later scp runs won't overwrite the log file of the previous run, but instead adds to it.

 

for i in hostname
do
    scp -r target files $i:/home/testuser >>statistics.log 2>>errors.log
done
exit

 

> i need to check if target files is  copied to target host.

 

The simplest way is to check the exit status of the scp command. "man scp" says it will be 0 if the copy operation is successful, and greater than 0 if not successful.

So:

for i in hostname
do
    scp -r target files $i:/home/testuser >>statistics.log 2>>errors.log
    if [ $? -gt 0 ]
    then
        echo "*** Copy to host $i failed ***" >>errors.log
    fi
done
exit

 

MK
Dennis Handly
Acclaimed Contributor

Re: scp by using script

>The only trick is that you probably should use >>

 

You can redirect the output of the whole loop and you won't need that ">>":

for i in hostname; do

   scp -r target files $i:/home/testuser

   if [ $? -ne 0 ] then

      echo "*** Copy to host $i failed ***" 1>&2

   fi

done > statistics.log 2> errors.log

 

Contrary to the man page, your check on $? should really be -ne instead of -gt.