【问题标题】:Passing arguments to remote script from local script using except使用 except 将参数从本地脚本传递给远程脚本
【发布时间】:2016-05-12 23:34:25
【问题描述】:

您好,我是 Expect 脚本的新手。我试图使用 ssh spawn 调用远程脚本并将命令行参数传递给远程脚本。但是在远程脚本中我得到空值。请帮助解决这个问题。在 except 脚本中传递参数有问题吗?

本地 Expect 脚本

#!/usr/bin/expect
set hana_schema [lindex $argv 1]
set table [lindex $argv 2]
set condition [lindex $argv 3]
set yyyymm [lindex $argv 4]
set targetdir [lindex $argv 5]
set split [lindex $argv 6]
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set script /path-to-script/test.sh
# here I spawn a shell that will run ssh and redirect the script to ssh's

spawn sh -c "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip bash $hana_schema $table $condition $yyyymm $targetdir $split < $script" "$hana_schema" "$table" "$condition" "$yyyymm" "$targetdir" "$split"
expect "Password:"
send "$password\r"

# and just wait for the script to finish
expect eof

远程脚本 test.sh

hana_schema=$1
table=$2
condition=$3
yyyymm=$4
targetdir=$5
split=$6
echo "$hana_schema"
echo "$table"
echo "$condition"
echo "$yyyymm"
echo "$targetdir"
echo "$split"

【问题讨论】:

    标签: ssh expect


    【解决方案1】:

    在这种情况下,您只需将期望脚本参数原封不动地传递给远程脚本。将它们保存在单独的变量中确实没有意义。也不需要用 sh 包装 ssh 调用。我会这样做:

    #!/usr/bin/expect
    set timeout 120
    set ip XXXX.XXX.XX.XX
    set user name
    set password pass
    set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no}
    set script /path-to-script/test.sh
    
    spawn ssh {*}$ssh_opts $user@$ip bash $script {*}$argv
    
    expect "Password:"
    send "$password\r"
    expect eof
    

    {*} 语法将列表扩展为各个元素。见http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm

    【讨论】:

    • 感谢@Glenn Jackman。它传递论点。并快速怀疑使用 spawn ssh 如何以 root 身份登录。
    • 命令行参数存储在全局 $argv 列表中,因此您可以使用 myproc $argv 传递单个列表,或使用 myproc {*}$argv 传递所有单个元素,或者您可以调用不带参数的 myproc 并在其中执行 proc myproc {} { global argv; foreach arg $argv {...
    • 在将参数传递给过程错误时出现以下错误#args:在执行“auto {*}argv”时应该是“auto”(文件“./expect_test.sh”第 16 行)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2020-11-19
    • 1970-01-01
    • 2016-05-04
    • 2017-12-13
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多