リモートSSHのパイプエコー

リモートSSHのパイプエコー

私が書いていないスクリプトがあります。実行されると、一部の情報が印刷され、ユーザーはEnterキーを押して期待して情報の最後の部分を印刷します。これが私に必要なものです。プログラムで最後の部分、つまり出力の最後の行を取得する必要があります。次のようにローカルでスクリプトを実行すると、次のようになります。

RESULT=$(echo -ne '\n' | script $param)  

出力を取得して処理できますが、同じ出力をリモートで実行しようとすると、つまり

RESULT=$(echo -ne '\n' | ssh remoteserver script $param)  

スクリプトが停止します。新しいラインのパイプがリモートSSHで動作しないようです。

この問題をどのように解決できますか?

修正する:
スクリプトはターミナルから直接入力を受け取り、場合に備えてPerlスクリプトです。

ベストアンサー1

端末を偽造し、必要なデータを「入力」します。まず、testproggieリモートシステムでテストプログラムを起動します。

#!/usr/bin/env perl
use 5.14.0;
use warnings;

say "one thing";

open my $fh, '<', '/dev/tty' or die "nope on /dev/tty: $!\n";
readline $fh;

say "another thing";

改行文字をリモートで使用すると失敗します。

$ printf "\n" | ssh test.example.edu ./testproggie
one thing
nope on /dev/tty: No such device or address
$ 

これで、remotenlローカルシステムから端末を偽造します。

#!/usr/bin/env expect

#set timeout 999
#match_max 99999

# this assumes the remote side does not do anything silly with
# the shell; if it does you may need to spawn a remote shell
# and then {send "./testproggie\r"} to that and then...
spawn -noecho ssh -q -t test.example.edu ./testproggie
# this can be improved if you know what the line before the
# wait-for-the-return-key will contain
expect -re .
send "\r"
expect eof

# this could be simplified with better expect calls, above
regexp {([^\r\n]+)\r\n$} $expect_out(buffer) unused lastline
puts ">>>$lastline<<<"

そしてそれを実行

$ ./remotenl
one thing

another thing
>>>another thing<<<
$ 

おすすめ記事