MaestroGarcia
MaestroGarcia

Reputation: 109

Using rsync in expect script with find -ctime give me an error?

I need some help with an expect script and rsync. When I run rsync in command line like this it works fine:

rsync -avz [email protected]:'$(find /cluster/storage/nobackup/coremw/var/log/saflog/FaultManagementLog/alarm/ -ctime -1)' /home/imstest/shared/Generate/ReportingT3/tmp/

But when I use my expect script that looks like this I get an error:

#!/usr/bin/expect -f
set host_ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password [lindex $argv 2]
set scp_remote_directory [lindex $argv 3 ]
#set scp_remote_filename [lindex $argv 4] 
set local_directory [lindex $argv 4]

set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
#spawn scp $user@$host_ip:$scp_remote_directory/$scp_remote_filename $local_directory/
spawn rsync -azv $user@$host_ip:\'\$(find $scp_remote_directory -ctime -1)\' $local_directory/
#spawn scp $user@$host_ip:$scp_remote_directory/CscfHealthCheckReport_$TagID_$phase* $local_directory/
match_max 100000
expect {
  -exact "Are you sure you want to continue connecting (yes/no)? " {
    send -- "yes\r"
    exp_continue
  }
  -exact "${user}@${host_ip}'s password: " {
    send -- "$password\r"
  }
  -exact "Password: " {
    send -- "$password\r"
  }
}
expect eof

The script name is td_general_scp.exp and i run it like this:

./td_general_scp.exp 10.33.122.22 root rootroot /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log /home/imstest/shared/Generate/ReportingT3/tmp

But then i get this error:

spawn rsync -azv [email protected]:'$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log -ctime -1)' /home/imstest/shared/Generate/ReportingT3/tmp/
Unexpected local arg: /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log
If arg is a remote file/dir, prefix it with a colon (:).
rsync error: syntax or usage error (code 1) at main.c(1362) [Receiver=3.1.0]
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./td_general_scp.exp" line 36)

But the spawn printout looks exactly the same as when i run it in command line? What am i doing wrong?

Thanks for the help!

I addded the comments from @Shubhangi Pardeshi but now i get this error:

spawn rsync -azv [email protected]:'$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log -ctime -1 )' /home/imstest/shared/Generate/ReportingT3/tmp/
Password: 
receiving incremental file list
rsync: change_dir "/root//$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm" failed: No such file or directory (2)

The scripts looks like this now:

#!/usr/bin/expect -f
set host_ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password [lindex $argv 2]
set scp_remote_directory [lindex $argv 3 ]
set local_directory [lindex $argv 4]

set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
#spawn scp $user@$host_ip:$scp_remote_directory/$scp_remote_filename $local_directory/
spawn rsync -azv $user@$host_ip:\'\$(find\ $scp_remote_directory\ -ctime\ -1\ )\' $local_directory/
#spawn scp $user@$host_ip:$scp_remote_directory/CscfHealthCheckReport_$TagID_$phase* $local_directory/
match_max 100000
expect {
  -exact "Are you sure you want to continue connecting (yes/no)? " {
    send -- "yes\r"
    exp_continue
  }
  -exact "${user}@${host_ip}'s password: " {
    send -- "$password\r"
  }
  -exact "Password: " {
    send -- "$password\r"
  }
}
expect eof

Here is the printout i gert when using exp_internal 1 after channging to @Shubhangi Pardeshi suggestion.

spawn rsync -azv [email protected]:'$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log -ctime -1 )' /home/imstest/shared/Generate/ReportingT3/tmp/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {115454}

expect: does "" (spawn_id exp6) match exact string "Are you sure you want to continue connecting (yes/no)? "? no
"[email protected]'s password: "? no
"Password: "? no
Password: 
expect: does "Password: " (spawn_id exp6) match exact string "Are you sure you want to continue connecting (yes/no)? "? no
"[email protected]'s password: "? no
"Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Password: "
send: sending "rootroot\r" to { exp6 }

receiving incremental file list
rsync: change_dir "/root//$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm" failed: No such file or directory (2)

sent 8 bytes  received 159 bytes  111.33 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [Receiver=3.1.0]
rsync: [Receiver] write error: Broken pipe (32)
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\nreceiving incremental file list\r\nrsync: change_dir "/root//$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm" failed: No such file or directory (2)\r\n\r\nsent 8 bytes  received 159 bytes  111.33 bytes/sec\r\ntotal size is 0  speedup is 0.00\r\nrsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [Receiver=3.1.0]\r\nrsync: [Receiver] write error: Broken pipe (32)\r\n"
imstest@TCA-Hubba3:~/shared/Generate/ReportingT3/bin$ 

I missed to chang to ` as the suggestion but now i have the exact row as suggested:

spawn rsync -azv $user@$host_ip:\`\$(find\ $scp_remote_directory\ -ctime\ -1\ )\` $local_directory/

But now I get a permission denied output? I logged in as root it can't be a permission problem. The error output looks like this.

spawn rsync -zv [email protected]:`$(find /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/Fm*.log -ctime -1 )` /home/imstest/shared/Generate/ReportingT3/tmp/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {118075}

expect: does "" (spawn_id exp6) match exact string "Are you sure you want to continue connecting (yes/no)? "? no
"[email protected]'s password: "? no
"Password: "? no
Password: 
expect: does "Password: " (spawn_id exp6) match exact string "Are you sure you want to continue connecting (yes/no)? "? no
"[email protected]'s password: "? no
"Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Password: "
send: sending "rootroot\r" to { exp6 }

bash: /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/FmAlarmLog_20160422_152507.log: Permission denied

sent 8 bytes  received 5 bytes  8.67 bytes/sec
total size is 0  speedup is 0.00
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\nbash: /cluster/storage/no-backup/coremw/var/log/saflog/FaultManagementLog/alarm/FmAlarmLog_20160422_152507.log: Permission denied\r\n\r\nsent 8 bytes  received 5 bytes  8.67 bytes/sec\r\ntotal size is 0  speedup is 0.00\r\n"
imstest@TCA-Hubba3:~/shared/Generate/ReportingT3/bin$ 

Upvotes: 0

Views: 522

Answers (1)

Shubhangi
Shubhangi

Reputation: 2264

Below two changes in rsync command in expect script will resolve issue

  1. Escape space in find command- $(find $scp_remote_directory -ctime -1)
  2. replace ' by `

Hence, spawn statement in expect script would look like

spawn rsync -azv $user@$host_ip:\`\$(find\ $scp_remote_directory\ -ctime\ -1\ )\` $local_directory/

Upvotes: 2

Related Questions