
私は 100 台のサーバーを持っており、スクリプトを使用して中央サーバーから SSH でそれらのサーバーにログインする必要があります。以下を試したところ、中央サーバーに保存されるファイルにリダイレクトされるバージョンが取得されるはずです。
#!/bin/bash
CMD='java -version'
while read line
do
ssh -n user@"$line" $CMD >> /pathforoutputfile/outputjava.txt
done < /pathforhosts/hosts.txt
しかし、ファイルに出力が生成されません/pathforoutputfile/outputjava.txt
答え1
そのコマンドは実際には stderr に書き込みます。
ron@haggis:~$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)
ron@haggis:~$
ron@haggis:~$ java -version 2> foo.txt
ron@haggis:~$ cat foo.txt
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)
したがって、in2>>
の代わりにを使用してリダイレクトする必要があります。>>
ssh -n sgarole@"$line" $CMD >> /pathforoutputfile/outputjava.txt
もう一つ気づいたのは、 でリモート ホスト名について言及していないようです/pathforoutputfile/outputjava.txt
。