您好,我是新手,期待脚本编写。我试图使用ssh spawn调用远程脚本并将命令行参数传递给远程脚本,但是在远程脚本中我得到了空值。请帮助解决此问题。在脚本中传递参数是否存在问题?
本地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"
在这种情况下,您只是将期望脚本参数传递给未修改的远程脚本。将它们保存在单独的变量中确实没有意义。同样也不需要用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句