2017-12-22 11 views
0

私は私のホストコンピュータに私のリモートサーバーから複数のファイルを取得するために、この小さなスクリプトを書いた:spawnコマンドのexpectプログラムに複数の行を書き込むにはどうすればよいですか?

#! /usr/bin/expect -f 

spawn scp \ 
[email protected]:/home/user/{A.txt,B.txt} \ 
/home/user/Documents 
expect "password: " 
send "somesecretpwd\r" 
interact 

これは正常に動作しているが、私はこのようなファイルの間に新しい行を作りたいとき:

[email protected]:/home/user/{A.txt,\ 
B.txt} \ 

私は次のようなエラー(s)は取得しています:

012:

scp: /home/user/{A.txt,: No such file or directory 
scp: B.txt}: No such file or directory 

を私はこれを試してみました

[email protected]:"/home/user/{A.txt,\ 
B.txt}" \ 

取得:

bash: -c: line 0: unexpected EOF while looking for matching `"' 
bash: -c: line 1: syntax error: unexpected end of file 
cp: cannot stat 'B.txt}"': No such file or directory 

またはこの:

"[email protected]:/home/user/{A.txt,\ 
B.txt}" \ 

を先頭に同じエラーを取得。

複数の行にファイルを書き込むことはできますが、プログラムが正しく動作するようにするにはどうすればよいですか?選択したファイルを読みやすくするために必要です。

+0

私は 'sshpass'を使用することをお勧め:ここ

コードがあります。 – Cyrus

+0

'sshpass 'の私の問題は、マルチホップのために複雑な引数を渡さなければならないということです(私は少なくとも2回sshを2回使用する必要があります)。 – PiMathCLanguage

答えて

0

だけ期待して使用して同様の問題を解決したい人にとって:

ファイルのリストを作成し、すべてのファイルを1つの文字列に連結することができます。

#! /usr/bin/expect -f 

set files {\ # a list of files 
A.txt\ 
B.txt\ 
C.txt\ 
} 

# will return the concatenated string with all files 
# in this example it would be: A.txt,B.txt,C.txt 
set concat [join $files ,] 

# self made version of concat 
# set concat [lindex $files 0] # get the first file 
# set last_idx [expr {[llength $files]-1}] # calc the last index from the list 
# set rest_files [lrange $files 1 $last_idx] # get other files 
# foreach file $rest_files { 
#  set concat $concat,$file # append the concat varibale with a comma and the other file 
# } 
# # puts "$concat" # only for testing the output 

spawn scp \ 
[email protected]:/home/doublepmcl/{$concat} \ 
/home/doublepmcl/Documents/programs/Python/neural_network/ 
expect "password: " 
send "somesecretpwd\r" 
interact 
+1

中間セクションを単純化すると、 'set concat [join $ files、]' - https://www.tcl.tk/man/tcl8.6/TclCmd/join.htmを参照してください。また、 "files"リストのエントリ間のバックスラッシュは、改行を含む空白を使用してリスト要素を区切ることができるため、冗長です。 –

+0

短いコードをありがとう、私は私の答えでこれを追加します。 – PiMathCLanguage

1

Tclでは(したがってExpect)\<NEWLINE><SPACEs>は単一の<SPACE>に変換されるので、スペースを含まない文字列を複数の行に書き込むことはできません。ファイル名が長い実際にあると仮定すると、

% puts "abc\ 
     def" 
abc def 
% puts {abc\ 
     def} 
abc def 
% 
+0

ああ良い説明。読解可能なコードを持つexpectスクリプトで特定のファイルのみを送信するには、必要なexpectスクリプトを作成する別のスクリプト(たとえばPython)を書くことができます。 "\ n \ n "は ""になりますか? – PiMathCLanguage

+1

'\ 'のような*スペース*は' s'(0x20)と 's'(0x09)のみを含みます。 – pynexj

1

(あまりない点はそうでない)あなたはこのような変数のカップルを使用することができます

#! /usr/bin/expect -f 

set A A.txt 
set B B.txt 

spawn scp \ 
[email protected]:/home/user/{$A,$B} \ 
/home/user/Documents 
expect "password: " 
send "somesecretpwd" 
interact 
+0

いくつかのファイルの場合、これは一時的な解決策として使用できます。 – PiMathCLanguage

関連する問題