代码之家  ›  专栏  ›  技术社区  ›  Abe Voelker

expect tcl脚本-使用spawn传递带引号的参数时出错

  •  2
  • Abe Voelker  · 技术社区  · 14 年前

    我刚刚写了一个非常简单的预期脚本来包装rsync,但它似乎给我带来了麻烦。基本上,我正在自动执行从rsync调用的ssh登录提示。我还必须通过rsync将参数传递给ssh,这样它就不会进行主机密钥检查。我很清楚ssh身份验证密钥和ssh keygen,但是我有很好的理由这样做,所以没有关于在命令行上传递密码的讲座。

    脚本


    #!/usr/local/bin/expect -f
    
    if {$argc != 5} {
      puts "usage: remoteCopy {remotehost, username, password, localfile, remoteloc}"
      exit 1
    }
    
    set remotehost [lindex $argv 0]
    set username [lindex $argv 1]
    set password [lindex $argv 2]
    set localfile [lindex $argv 3]
    set remoteloc [lindex $argv 4]
    
    set timeout -1
    
    spawn rsync -e \"ssh -q -o StrictHostKeyChecking=no\" $localfile $username@$remotehost:$remoteloc
    expect "Password"; send "$password\r"
    

    以下是脚本的完整输出:

    产量


    avoelker@localhost  $ ./remoteFileCopy.tcl remotehost remoteuser remotepass ~/localfile /tmp/
    spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/
    rsync: Failed to exec "ssh: No such file or directory (2)
    rsync error: error in IPC code (code 14) at pipe.c(83)
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: error in rsync protocol data stream (code 12) at io.c(434)
    send: spawn id exp6 not open
        while executing
    "send "$password\r""
        (file "./remoteFileCopy.tcl" line 17)
    avoelker@localhost  $
    

    我觉得rsync正在尝试执行 "ssh 而不仅仅是 ssh 但是,从expect的stdout复制和粘贴输出的第一行( rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/ )直接进入shell非常好,所以我知道rsync没有问题。

    当我将-e参数从 spawn rsync 行中,预期脚本执行时没有错误,但我必须向脚本添加主机键检查,我不想这样做。

    有没有比Expect/TCL更有经验的人知道我做错了什么?

    1 回复  |  直到 14 年前
        1
  •  6
  •   Daniel Martin    14 年前

    放下你的反斜杠。这个 spawn 行应该只说:

    spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" $localfile $username@$remotehost:$remoteloc
    

    毕竟,你不想 rsync 要查看包含引号的命令,是吗?