在find -ctime的expect脚本中使用rsync会给我一个错误吗?

时间:2016-04-22 04:58:43

标签: bash scripting expect rsync

我需要一些期望脚本和rsync的帮助。 当我在命令行中运行rsync时,它工作正常:

rsync -avz root@10.33.122.22:'$(find /cluster/storage/nobackup/coremw/var/log/saflog/FaultManagementLog/alarm/ -ctime -1)' /home/imstest/shared/Generate/ReportingT3/tmp/

但是当我使用看起来像我的expect脚本时,我得到一个错误:

#!/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

脚本名称是td_general_scp.exp,我这样运行:

./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

但后来我收到了这个错误:

spawn rsync -azv root@10.33.122.22:'$(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)

但是,生成的打印输出看起来与在命令行中运行它时完全一样? 我做错了什么?

感谢您的帮助!

我添加了来自@Shubhangi Pardeshi的评论,但现在我收到了这个错误:

spawn rsync -azv root@10.33.122.22:'$(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)

脚本现在看起来像这样:

#!/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

这是打印到@Shubhangi Pardeshi建议后使用exp_internal 1时的打印输出。

spawn rsync -azv root@10.33.122.22:'$(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
"root@10.33.122.22'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
"root@10.33.122.22'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$ 

我错过了改为`作为建议,但现在我有了建议的确切行:

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

但现在我获得了拒绝输出的权限?我以root身份登录它不能是权限问题。错误输出如下所示。

spawn rsync -zv root@10.33.122.22:`$(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
"root@10.33.122.22'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
"root@10.33.122.22'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$ 

1 个答案:

答案 0 :(得分:2)

在expect脚本中,rsync命令中的两个更改将解决问题

  1. find命令 - $(find $scp_remote_directory -ctime -1)
  2. 中的转义空间
  3. 替换'by`
  4. 因此,expect脚本中的spawn语句看起来像

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