I have a script in which i am reading source and destination IP address from a csv file and recording the time in milliseconds it is taking to copy the file from source to destination (using scp). Below is my script.

while IFS="," read f1 f2
        echo "Source IP        : $f1"       
        echo "Destination IP   : $f2"
export sourceIP=$f1
export destIP=$(echo "$f2" | tr -d '\n')
ssh -t -t sjain@$f1 'bash -s' <<ENDSSH
#Start copying 100MB File
startTime=$(($(date '+%s%N')/1000000))
echo \$startTime
scp MB_100.txt sjain@$destIP:/home/sjain
endTime=$(($(date '+%s%N')/1000000))
echo \$endTime
printf 'Elapsed time in copying 33KB file: %s\n' \$((endTime-startTime)) 
#Write the stats to the file
echo $sourceIP','$destIP',33KB,'\$((endTime-startTime)) >> report.txt

#Write the stats to the file
#echo ',,33KB,'\$((endTime-startTime)) >> report.txt

done < ipaddress.csv


[sjain@XYZ ~]$ #Start copying 100MB File
[sjain@XYZ ~]$ startTime=1394659673854
[sjain@XYZ ~]$ echo $startTime
[sjain@XYZ ~]$ scp MB_100.txt sjain@10.X.X.X:/home/sjain
MB_100.txt                                    100%  100MB 100.0MB/s   00:00
[sjain@XYZ ~]$ endTime=1394659673855
[sjain@XYZ ~]$ echo $endTime
ndTime-startTime)) D01 ~]$ printf 'Elapsed time in copying 33KB file: %s\n' $((e
Elapsed time in copying 33KB file: 1
[sjain@XYZ ~]$ #Write the stats to the file
Time)) >> report.txt01 ~]$ echo 10.Y.Y.Y','10.X.X.X',33KB,'$((endTime-start
[sjain@XYZ ~]$ exit
Connection to 10.Y.Y.Y closed.

The problem that I am facing right now , it is not returning me the time in milliseconds (i think) , it is giving in seconds.

Please help in fixing this issue.


You'd better use time.

For example

time (scp yourfile user@otherhost:/path/ ) 2>$TMP
awk -F'[ ms]+' '/^real/ {print "copy time: "1000*$2"ms"}' $TMP
rm $TMP
  • i think in COPYTIME expression , 1 single quote is missing. i am getting this error ./dummy.sh: command substitution: line 57: unexpected EOF while looking for matching '' ./dummy.sh: command substitution: line 59: syntax error: unexpected end of file` – saurav Mar 13 '14 at 14:42

You can use the scp in verbose mode and grab the timing stats. Replace your scp line as follows:

scp -v src dest 2>&1 | grep 'Transferred' | awk '{print $(NF - 1) * 1000}'

Edit: This might not be precise to the millisecond.

With python, one can get microsecond level precision, eg.

#!/usr/bin/env python

import subprocess
import datetime

subprocess.call(["scp", "-r", "a-dir/", "username@machine.name:~/"])
print (b-a).microseconds
  • What OS is this on? My scp (Debian) does not print this info. – terdon Mar 13 '14 at 3:00
  • @terdon this is BSD scp on Ubuntu "precise 12.04.xx". – mkc Mar 13 '14 at 14:57
  • Never mind, I was doing something stupid. It does indeed print this on my Debian. – terdon Mar 13 '14 at 15:06
  • @ketan is there anyway to make it precise ? – saurav Mar 13 '14 at 15:20
  • 1
    @tech-idiot This can be done with python if that is acceptable to you. See my updates on answer. – mkc Mar 13 '14 at 15:48

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.