使用except从本地脚本将参数传递给远程脚本

Passing arguments to remote script from local script using except

您好,我是 Expect 脚本编写的新手。我正在尝试使用 ssh spawn 调用远程脚本并将命令行参数传递给远程脚本中的远程 script.But 我正在获取空值。请帮助解决这个问题。在 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=
table=
condition=
yyyymm=
targetdir=
split=
echo "$hana_schema"
echo "$table"
echo "$condition"
echo "$yyyymm"
echo "$targetdir"
echo "$split"

在这种情况下,您只是将预期脚本参数原封不动地传递给远程脚本。将它们保存在单独的变量中真的没有意义。也不需要用 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