2016-05-12 123 views
0

Hello Im newbie to Expect scripting。我試圖使用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 [email protected]$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" 

回答

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 [email protected]$ip bash $script {*}$argv 

expect "Password:" 
send "$password\r" 
expect eof 

{*}語法將列表擴展爲其各個元素。請參閱http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm

+0

謝謝@Glenn Jackman。它傳遞參數。像通過使用spawn ssh如何以root身份登錄一樣,很容易懷疑。 – marjun

+0

如何將命令行參數傳遞給tcl中的過程? – marjun

+1

命令行參數存儲在全局'$ argv'列表中,所以你可以通過'myproc $ argv'來傳遞一個列表,或者'myproc {*} $ argv'來傳遞所有的單個元素,或者你可以不帶任何參數地調用myproc,並在裏面執行'proc myproc {} {global argv; foreach arg $ argv {...' –